使用javascript访问子元素 [英] accessing a child element using javascript

查看:75
本文介绍了使用javascript访问子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的先生,

我已经动态创建了控件,并且控件位于表内部和div内部表中.在mozilla浏览器中出现错误..

示例html生成的代码如下所示

Dear sir,

I have created controls dynamically and the controls are inside the table and the table inside the div.Im getting error in mozilla browser..

The sample html generated code is shown below

<div id=divall  runat="server">
<table cellpadding="0" border="0" id="tblall">
<tr>
<td><span id="lblas">sdfssdf</span></td>
<td>
<select id=dd1>
<option value="1">sdfsd</option>
<option value="1">sdfsd</option>
<option value="1">sdfsd</option>
<option value="1">sdfsd</option>
</select>
</td>
</tr>
<tr>
<td><span id="lblas">sdfssdf</span></td>
<td>
<select id=dd1>
<option value="1">sdfsd</option>
<option value="1">sdfsd</option>
<option value="1">sdfsd</option>
<option value="1">sdfsd</option>
</select>
</td>
</tr>

<tr>
<td><span id="lblas">sdfssdf</span></td>
<td>
<select id=dd1>
<option value="1">sdfsd</option>
<option value="1">sdfsd</option>
<option value="1">sdfsd</option>
<option value="1">sdfsd</option>
</select>
</td>
</tr>

</table>



我访问以下值

var divall = document.getElementById(''divall'');

//用于将所有控件放入div



I access the value as below

var divall = document.getElementById(''divall'');

//for getting all the controls inside the div

var allcntr = divall.childNodes[0].childNodes[0].childNodes;
for(var i=0;i<allcntr.length;i++)>
{
//for getting all the dropdownlist''s value inside the table

var cmball=divall.childNodes[0].childNodes[0].childNodes[i].getElementsByTagName(''OPTION'');

for(var j=0;j<cmball.length;j++)>
{
if(cmball[j].selected)
{
  //some code
}
}


}


//


//

<br />
divall.childNodes[0].childNodes[0].childNodes;


上面的代码在IE中工作5n,但在mozilla中不工作.
是否有用于访问子节点的通用代码(兼容所有主要是IE& Mozilla的浏览器)?

感谢您的帮助..

除了原型之外,还有其他替代方法吗?


The above code is working 5n in IE but not in mozilla.
Is there any common code(compatible to all browsers mainly IE & Mozilla) to access the childnodes?

Thanx for your help in advance..

Is there any other alternate besides prototypes?

推荐答案

我的建议是使用一些跨浏览器库. 我更喜欢原型,其他人可能会说jQery或
其他任何lib都在那里闲逛.
这些库确实减轻了痛苦
跨浏览器兼容性.

对不起,忘记了我喜欢的链接:
http://www.prototypejs.org/

干杯

曼弗雷德(Manfred)
My advice is to use some cross browser library.
I prefer Prototype others might say jQery or
whatever else libs hang around out there.
These libraries really take the pain out of
cross browser compatibility.

Sorry forgot the link to my favorite:
http://www.prototypejs.org/

Cheers

Manfred


这篇关于使用javascript访问子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆