2011-11-03 67 views
1

我正在尋找的代碼將焦點設置文本框在iframe中,我發現這樣的代碼將焦點設置的iFrame jQuery的內部控制

//get the IFRAME element - note no hashes in the name, we're using browser functionality 

var iframeRef = document.getElementById("IFRAMEID"); 
//focus the IFRAME element 
$(iframeRef).focus(); 
//use JQuery to find the control in the IFRAME and set focus 
$(iframeRef).contents().find("#CONTROLID").focus(); 

但很少有人說這個代碼不工作....所以這將是正確的代碼。謝謝

+0

此iframe內容頁面是否與父頁面具有相同的域? –

回答

1

只有當您的iframe與父窗口具有相同的域時,它纔會起作用。

+0

$(iframeRef).contents()。find(「#CONTROLID」)。focus(); 什麼是.contents()在這裏.... plzz解釋 –

+0

'contents()'接受iframe窗口對象並本地化它以遍歷。 – AlienWebguy