在另一个框架的上下文中运行JQuery [英] Run JQuery in the context of another frame

查看:79
本文介绍了在另一个框架的上下文中运行JQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < frameset rows =100,* ,0> 
< frame name =theFrameid =theFramesrc =blah.html>
< frame name =theSecondFrameid =theSecondFramesrc =foo.html>
< frame name =importantFrameid =importantFramesrc =myFrame.html>
< / frameset>

当某个动作发生时,我需要我的框架(当前隐藏的重要框架)该页面并阻止与其他框架的任何交互。我正计划使用jquery block UI插件阻止交互。



问题是我实际上无法更改foo.html或blah.html文件。所以JS代码不能住在那里。 我需要做的是在这些框架的上下文中执行我的jquery代码。所以我只想回顾一下,我需要我的JQuery代码来生活在myFrame.html中,但是在其他框架的上下文中执行。我怎样才能做到这一点?希望这是有道理的。



感谢
CDR

解决方案

jQuery 函数更常用于调用 $ ,它接受第二个参数context,即which我应该在搜索中使用jQuery对象的DOM元素。大多数时候你忽略了这个参数,所以上下文默认为当前的HTML文档。当您的代码在iframe中执行时,文档默认为iframe的文档。但是您可以轻松获取其他框架之一的文档。例如,把它放在 myFrame.html 中,这会从框架中删除所有的h1元素, blah.html 就可以了。注意 $ 的第二个参数,它是从重要帧中抓取blah帧的表达式:

 < HTML> 
< head>
< script type =text / javascriptsrc =/ javascripts / jquery.js>< / script>
< script type =text / javascript>
函数doIt(){
$('h1',window.parent.frames [0] .document).remove()
}
< / script>
< / head>
< body>
< h1>我的框架< / h1>
< a href =#onclick =doIt()>完成< / a>
< / body>
< / html>


The client I am working with has a frameset like so...

<frameset rows="100,*, 0">
  <frame name="theFrame" id="theFrame" src="blah.html" >
  <frame name="theSecondFrame" id="theSecondFrame" src="foo.html" >
  <frame name="importantFrame" id="importantFrame" src="myFrame.html" >
</frameset>

When a certain action takes place I need my frame (importantframe which is currently hidden) to mostly take over the page and block any interaction with the other frames. I'm planning on blocking interaction using the jquery block UI plugin.

The problem is I can't actually change the foo.html or blah.html files. So the JS code cannot live there. What I need to do is execute my jquery code in the context of those frames. So just to recap, I need my JQuery code to live in myFrame.html but execute in the context of the other frames. How can I do that? Hope that makes sense.

Thanks CDR

解决方案

The jQuery function, which you more commonly call with $, takes a second argument called context, which is "which DOM element of jQuery object should I do the search in". Most of the time you omit this argument, so the context defaults to the current HTML document. When your code is executing in the iframe, the document defaults to that iframe's document. But you can easily grab the document for one of the other frames.

For example, put this in myFrame.html, and this will remove all the h1 elements from the frame with blah.html in it. Notice the second argument to $, which is the expression that grabs the blah frame from within important frame:

<html>
  <head>
    <script type="text/javascript" src="/javascripts/jquery.js"></script>
    <script type="text/javascript">
      function doIt() {
        $('h1', window.parent.frames[0].document).remove()
      }
    </script>
  </head>
  <body>
    <h1>My Frame</h1>
    <a href="#" onclick="doIt()">Do It</a>
  </body>
</html>

这篇关于在另一个框架的上下文中运行JQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆