jQuery与iframe和parent冲突

时间:2012-06-26 09:16:35

标签: javascript jquery html jquery-plugins

我正在使用jQuery加载函数将外部页面(在我的域中相同)加载到div中。这两个页面也将具有单独的角色,即子页面具有其自己的功能(作为单独的页面工作),并且它也将加载到div中。页面结构为

第1页(父母)

<html>
<head>
<script>JQUERY HERE</script>
</head>
<body>
<div id="to_load_second_page"></div>
</body>
</html>

第二页

<html>
<head>
<script>JQUERY HERE</script>
</head>
<body>
<div id="contents"></div>
</body>
</html>

单独两个页面都正常工作。但是一旦在1st的div中加载第二页后,第一页的jQuery函数就不起作用了。

我认为当孩子再次加载第二页时,jQuery.js会被添加到父级中。但是我们不能避免加载这个jquery.js,因为子页面也需要为jquery.js单独运行。

请帮助解决此问题

感谢您的时间 :)

1 个答案:

答案 0 :(得分:0)

当使用jQuery的.load()将内容附加到现有元素时,您不希望获取整个HTML文档,而只需要在现有页面中注入新标记。但请注意,jQuery将确保所提取的HTML片段中的<script>标记在全局上下文中执行(“第1页”,您已经有jQuery可用!)所以你可以像这样组织第2页的内容:

<div id="contents"><!-- Contents here --></div>
<script>
    /* JS you want to execute - but don't include jQuery.js here - 
       it's already available and including it again will overwrite the
       existing object, making any plugins originally included unavailable!
     */
</script>