0

我有一个包含几个 div 和其他元素的 iframe。我想将焦点设置到几个文本框中的一个文本框。

我用了:

    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 这两种方法有什么区别吗?

4

3 回答 3

0

检查这个

$("input[id$='myTxtBox']").val()
于 2013-02-20T12:06:06.880 回答
0

试试这个

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

使用 jquery...

于 2013-02-20T12:13:17.937 回答
0

getElementsByClassName 方法只在 IE9+ 上可用,所以错误信息是正确的(虽然不是很清楚),在 IE8 上没有这种方法。

您可以在此处阅读更多信息:http: //msdn.microsoft.com/en-us/library/ie/ff975198 (v=vs.85).aspx

于 2013-02-20T12:24:20.227 回答