2013-02-20 37 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()