Javascript jquery 选择 iframe 子级
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2893280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery select iframe children
提问by Val
I am using the editArea library and jquery to do what i need...
我正在使用 editArea 库和 jquery 来做我需要的......
http://www.cdolivet.com/index.php?page=editArea&sess=2b8243f679e0d472397bfa959e1d3841
http://www.cdolivet.com/index.php?page=editArea&sess=2b8243f679e0d472397bfa959e1d3841
so in my html there is an iframe tag that editArea uses what i need is to access something like so with jquery
所以在我的 html 中有一个 iframe 标签,editArea 使用我需要的是使用 jquery 访问类似的东西
$('iframe textarea').keydown(function (e){
number = 17; //any number really :)
if(e.which == number){
//do something...
alert('Done...');
}
});
I tried the above but it looks like it is not detecting that key. but it works if selector was $(document) therefore the rest of the function works it's just it's not picking up the iframes textarea keydown
我尝试了上面的方法,但看起来它没有检测到那个键。但如果选择器是 $(document) 则它起作用,因此该函数的其余部分起作用,只是它没有拾取 iframes textarea keydown
any ideas? Thanks
有任何想法吗?谢谢
回答by jAndy
$("iframe").contents().find("textarea").keydown(...)
回答by CMS
You need search within the inner documentof the iframe, in order to get the textareaelement:
您需要在documentiframe的内部进行搜索,以获取textarea元素:
var textarea = $('textarea', $('iframe').get(0).contentWindow.document);
textarea.keydown(function (e){
var number = 17;
var code = (e.keyCode ? e.keyCode : e.which);
if(code == number){
//do something...
alert('Done...');
}
});
回答by Vins
You can travel any level, The above answers are correct. I did as below
您可以旅行任何级别,以上答案是正确的。我做了如下
var iframeMain = $("#iframe0").contents().find('#iframeMain');
var detailsForm = $(iframeMain).contents().find('#detailsform');
for accessing parents you can use
访问父母,您可以使用
window.parent.parent. // upto any level
回答by Sean Kinsey
You need to access the iframes contentWindow.document, not the iframe itself.
您需要访问 iframes contentWindow.document,而不是 iframe 本身。
回答by sushil bharwani
You should be doing
你应该做
iframeDoc = document.getElementById('resultsFrame').contentWindow;
// result frame is id of iframe
$(iframeDoc).keydown(function(e))
{
// your functionality here
}
回答by Adam Grant
If all else fails, I ended up getting the above to work by wrapping in this function:
如果一切都失败了,我最终通过包装在这个函数中使上述工作:
$(window).blur(function () {
// genius code here
}
回答by Manveer Singh
Below code helped me.
下面的代码帮助了我。
$(document).ready(function(){
var iFrameDOM = $("iframe#frameID").contents();
iFrameDOM.find(".page").css("background-color", "#fff");
});
});

