我有呈现以下 HTML 的代码
<tr>
<td>Control 1 with onclick="javascript:Test(this);" </td>
<td>Control 2</td>
<td><span><div>Control 3</div></span></td>
</tr>
function Test(myThis)
{
//Here I would like to get references to the siblings of Control 1 (in this ex: Control 2, Control 3)
//and disable them. Any ideas?
}
我正在使用这种查找同级的方法,因为这是在深度 = 3 的嵌套用户控件中,并且 ASP.NET 引用带有前缀的控件,这是非常困难的,特别是当我在页面上有多个控件实例时。
最佳答案
如果您使用 jQuery 之类的 javascript 库,事情就会变得非常简单:
$(this).siblings().disable();
http://docs.jquery.com/Traversing/siblings#expr
如果您没有 jQuery 或类似的库,那么您必须手动执行此操作:
function disableSiblingsOf(elem)
{
var nodes = elem.parent.childNodes;
for(var i=0; i<nodes.length; i++);
if(nodes[i] != elem)
nodes[i].disabled = true;
}
这应该可以解决问题。
关于asp.net - JavaScript 和 ASP.NET 控件前缀中的 "this",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/512588/