2013-02-20 67 views
0

我有一個包含多個div和其他元素的iframe。我希望將焦點設置爲幾個文本框中的一個文本框。無法通過id或className獲取元素

我用:

a = iFrameObj.contentWindow.document.getElementById('myTxtBox'); 

    But here, a is null; 

我能夠獲得訪問使用下面的代碼的文本框對象;

var myTextBox = iFrameObj.contentWindow.document.getElementsByTagName('input')[52]; 

但我想用更通用的方法來獲取對象而不是硬編碼索引。

由於該文本框具有獨特的類名,我嘗試下面的代碼:

var myTextBox = iFrameObj.contentWindow.document.getElementsByClassName('rgtxt')[0]; 

但我的錯誤:

"Object does not support this property or method" 

HTML我的文本框是:

<input name="myTxtBox" type="text" class="rgtxt" id="myTxtBox" value="hello" style="display:block;color:Black;background-color:rgb(240, 241, 241);" readonly="readonly" /> 

有人能幫助這兩種方法在iFrame中有什麼區別?

+1

http://stackoverflow.com/questions/1896360/getelementbyid-from-iframe – 2013-02-20 12:06:09

+0

,你能否告訴我們myTxtBox的HTML? – Adil 2013-02-20 12:08:29

+0

您使用哪個瀏覽器/版本來運行您的代碼? – 2013-02-20 12:10:30

回答

0

試試這個

$("#youriFrameID").contents().find("input.rgtxt").focus(); 

使用jQuery ...

0

入住這

$("input[id$='myTxtBox']").val()