2009-02-04 56 views
0

我有呈現下面的HTML「此」 javascript和ASP.NET控制前綴

<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? 

} 

我使用發現兄弟姐妹,因爲這是在與深度嵌套的usercontrol的這種方法的代碼= 3和ASP.NET引用的控件的前綴非常難,特別是如果我在頁面上有多個控件實例。

回答

5

,如果您使用的是JavaScript庫如jQuery變得非常簡單:

$(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; 
} 

這應該是個訣竅。

0

加入jQuery和使用以下命令:

$(myThis).nextAll("td").each(function(){ 
    // Code to "disable" them here, dunno what you mean by that though as they are tds. 
}); 
0

我假設你沒有一個Javascript庫可用,因爲我已經看到張貼的jQuery的答案。

它很大程度上取決於您的控件呈現的HTML。但是,您可能只需在循環中引用this.parent,直到this.tagName === 'TR'this.parent變爲空。然後下降。