找不到框架子项
我正在维护一个使用框架集/框架的旧网站。
我不想将处理程序附加到主框架,因此当用户单击主体某处时,会弹出警报。
我已经敲了好久的头了,但我现在感觉我已经很接近了,我可以尝到它的味道,但我仍然无法弄清楚问题所在。我正在使用jquery 1.5.1。
$(function () {
$($('frame[name="main"]')).ready(function () {
console.log($('frame[name="main"]')); // this is a success (I can view it in firebug
console.log($('frame[name="main"]').find('body')); // fails (nothing is found)
});
});
这是框架
<frameset cols="190,*" frameborder="no" border="0" framespacing="0" framepadding="0" marginwidth="0" marginheight="0">
<frame name="head" src="someHTMLPage1.html" scrolling="no" noresize frameborder="no" border="0" framespacing="0" framepadding="0" marginwidth="0" marginheight="0">
<frame name="main" src="someHTMLPage2.html" scrolling="auto" frameborder="no" border="0" framespacing="0" framepadding="0" marginwidth="0" marginheight="0">
</frameset>
I'm maintaining an old site that uses frameset/frames.
I wan't to attach a handler to the main frame so when the user clicks the body somewhere an alert will pop up.
I've been banging my head for a while now, but I now feel like I'm so close I can taste it, yet I still can't figure out the problem. I'm using jquery 1.5.1.
$(function () {
$($('frame[name="main"]')).ready(function () {
console.log($('frame[name="main"]')); // this is a success (I can view it in firebug
console.log($('frame[name="main"]').find('body')); // fails (nothing is found)
});
});
Here are the frames
<frameset cols="190,*" frameborder="no" border="0" framespacing="0" framepadding="0" marginwidth="0" marginheight="0">
<frame name="head" src="someHTMLPage1.html" scrolling="no" noresize frameborder="no" border="0" framespacing="0" framepadding="0" marginwidth="0" marginheight="0">
<frame name="main" src="someHTMLPage2.html" scrolling="auto" frameborder="no" border="0" framespacing="0" framepadding="0" marginwidth="0" marginheight="0">
</frameset>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
谢谢阿图罗,
您的解决方案有所帮助,但并不完美。这就是我所做的。
我必须使用
.load
而不是.ready
才能使其正常工作。这适用于 IE 7/8/8 兼容模式以及 FF4 和 Chrome 12(可能适用于早期版本的 FF 和 Chrome,我只是没有测试)
Thanks Arturo,
Your solution helped but wasn't perfect. Here's what I did.
I had to use
.load
instead of.ready
in order for this to work.This worked in IE 7/8/8-compatibility-mode and FF4 and Chrome 12 (probably works in earlier versions of FF and Chrome, I just didn't test)
首先,jquery Ready 不能很好地与它为文档设计的窗口配合使用。框架封装了窗口。
另外我认为你需要在框架内的页面上加载 jquery 才能找到正文。
尝试不使用 jQuery
另一件可能影响您的事情是框架中的页面与框架集不在同一域中。这将阻止框架集中的任何脚本。
您也可以更改为加载而不是就绪
$($('frame[name="main"]')).load(function() { ...
First of all, jquery ready doesn't work very well with window it was designed for document. A frame encapsulates a window.
Also i think you need to load jquery on the page inside your frame to find the body.
Try without jQuery
The other thing that can also affect you is if your page in the frame is not in the same domain as the frameset. This would block any script from the frameset.
Also you can change to load instead of ready
$($('frame[name="main"]')).load(function() { ...