[英]jquery select iframe children
我正在使用editArea庫和jquery來做我需要的...
http://www.cdolivet.com/index.php?page=editArea&sess=2b8243f679e0d472397bfa959e1d3841
所以在我的HTML中有一個iframe標簽,editArea使用我需要的東西來使用jquery訪問類似的東西
$('iframe textarea').keydown(function (e){
number = 17; //any number really :)
if(e.which == number){
//do something...
alert('Done...');
}
});
我嘗試了以上但看起來它沒有檢測到該密鑰。 但是如果選擇器是$(文檔)它可以工作,因此函數的其余部分工作它只是它沒有拿起iframes textarea keydown
有任何想法嗎? 謝謝
$("iframe").contents().find("textarea").keydown(...)
您需要在iframe的內部document
進行搜索,以獲取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...');
}
});
你可以旅行任何級別,以上答案是正確的。 我做了如下
var iframeMain = $("#iframe0").contents().find('#iframeMain');
var detailsForm = $(iframeMain).contents().find('#detailsform');
訪問父母,你可以使用
window.parent.parent. // upto any level
您需要訪問iframes contentWindow.document
,而不是iframe本身。
如果所有其他方法都失敗了,我最終通過包裝此函數來完成上述工作:
$(window).blur(function () {
// genius code here
}
你應該這樣做
iframeDoc = document.getElementById('resultsFrame').contentWindow;
// result frame is id of iframe
$(iframeDoc).keydown(function(e))
{
// your functionality here
}
下面的代碼幫助了我。
$(document).ready(function(){
var iFrameDOM = $("iframe#frameID").contents();
iFrameDOM.find(".page").css("background-color", "#fff");
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.