开发者社区> 问答> 正文

jQuery在iframe工作不正常

<!doctype html>
<html>
<head>
    <style type="text/css">
        //some details
    </style>
    <script type="text/javascript" src="./js/jquery-1.4.3.min.js"></script>
    <script type="text/javascript">
        $(function(){
            $(".teacherBtn").bind('click',function(){
                console.dir($("#questRange")); //it works when change the id to that occured in parent window (out of iframe).
                console.dir($(this));
                console.dir($(".teacherBtn"));
            });
        });
    </script>
</head>
<body>
    <div id="teacherCtrl">
        <form name="questOpt" id="questOpt"  action="questCtrl.php" method='post'>
            <div id="questRange" class='optSection'>
               //some details
            </div>
            <div id="questCtrl" class='optSection'>
               //some details
            </div>
            <div id='questType' class='optSection'>
               //some details
            </div>
            <input type="button" value="show" id="showPaper" name="showPaper" class="teacherBtn"/>
            <input type="button" value="download" id="downDoc" name="downDoc" class="teacherBtn" />
            <input type="button" value="submit" id="olTestOpt" name="olTestOpt" class="teacherBtn"/>
 
        </form>
    </div>
</body>
</html>

以上代码是以iframe的形式被载入到文档中(父文档称parent,此文档称iframe)。因为用了个jquery 的插件弹出窗口是用iframe的形式载入的。

现在的情况是,事件绑定正常,但是点击按钮后,选择器找不到相关的对象。将选择器改成parent中的元素的,就正常了。

我在另外的文档中测试,当iframe中重新载入了jquery库之后,$函数是在iframe文档中寻找,但是这里不知道有啥问题。在这里输出window的内容,也确实是iframe的window.

另外,如果我不在iframe中载入jq库,而是使用 $ = parent.$ 的形式引入$函数, $就会在parent中寻找元素。这个是为啥?我理解的$只是个函数而已。

展开
收起
a123456678 2016-07-06 15:21:40 2230 0
1 条回答
写回答
取消 提交回答
  • 发现用$("iframe")来取iframe不太稳定,经常无法取到;后来在iframe中加了一个id="frame",用jquery取$("#frame")就正常了

    2019-07-17 19:52:11
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
23-Vue.js在前端...1506518547.pdf 立即下载
Javascript中的函数 立即下载
JavaScript函数 立即下载