无法让我的脚本 (jquery) 将第 1 帧中的文本插入到第 2 帧的文本区域中。

Can't get my script (jquery) to insert text from frame 1 into textarea in frame 2.

我一直试图让它继续下去,但没有成功——执行高级搜索失败。当前设置如下:我不知道如何 link 框架在一起,所以当我单击第一框架中的列表时,文本被插入并显示在第二框架文本区域中。我知道必须有一种更简单的方法来做到这一点,我想念的。谢谢!

         index.html (frameset)
              |
              |
            /   \
           /     \
     group_a.html    group_b.html



**index.html**
<html>
<frameset cols="60%,40%" FRAMEBORDER=NO FRAMESPACING=0 BORDER=0>>
  <frame src="group_a.html">
  <frame src="group_b.html">
</frameset>
</html>

group_a.html
</style>
<style>
    li:hover {
        cursor: hand; cursor: pointer; background-color:#66CCFF
    }

</style>

<script type="text/javascript" src="jquery221min.js"></script>
<script>
$(document).ready(function(){
    $("li").click(function(){
        $('#alltext').append($(this).text());

        $("a").click(function(event){
            event.preventDefault();
            linkLocation = this.href;
    });
    function redirectPage() {
        top.frames["group_b.html"].location = linkLocation;
});
</script>
</head>
<body>

<ul>
    <li>Text choice 1</li>
    <li>Text choice 2</li>
    <li>Text choice 3</li>
    </ul>

</body>
</html>

 group_b.html
 <html>
  <body>

    <form>
      <textarea name="alltext" id="alltext" cols="60" rows="30">      </textarea>
  </form>

frame1 无法访问 frame2 的 DOM。这意味着您不能像您期望的那样使用 jQuery 操作文本区域。相反,使用 postMessage 在帧之间发送消息,然后在第二帧中为该消息设置一个侦听器。当你收到它时,相应地设置文本。类似于:

第 1 帧

var msg = {
   message: "updateText",
   text: $(this).text()
};

top.frames["group_b.html"].postMessage(JSON.stringify(msg), '*');

第 2 帧

window.addEventListener("message", receiveMessage, false);

function receiveMessage(event)
{ 
  if (event.data.message === "updateText"){
    $('#alltext').text(event.data.text);
  }
}

请注意,您必须将代码放在 jQuery 对象在范围内的地方。

在正常情况下 html https://jsfiddle.net/Cuchu/shs21jgs/ 您的 javascript 代码需要正确关闭。

$(document).ready(function(){
    $("li").click(function(){
        $('#alltext').append($(this).text());

        $("a").click(function(event){
            event.preventDefault();
            linkLocation = this.href;
          });
          function redirectPage() {
              top.frames["group_b.html"].location = linkLocation;
          };
    });
});