2014-04-29 25 views
0

我有這樣的結構如何通過屬性找到一個列表元素內的JQuery

HTML

<div> 
<ul id="categories"> 
<asp:Repeater ID="repeater" runat="server" OnItemDataBound="repeater_ItemDataBound"> 
<ItemTemplate> 
    <li><asp:LinkButton ID="linkbutton" runat="server" href="javascript:void(0);"/></li> 
</ItemTemplate> 
</asp:Repeater> 
</ul> 
</div> 

代碼背後

protected void repeater_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListItemType.Item || 
     e.Item.ItemType == ListItemType.AlternatingItem) 
    { 
     LinkButton linkbutton = (LinkButton)e.Item.FindControl("linkbutton"); 
     lnkCategoryName.Attributes.Add("data-category", ((ProductCategory)e.Item.DataItem).Description); 
    } 
} 

從開發者工具檢查元素

<ul id="categories"> 
<li><a id="cphContents_repeater_linkbutton_0" href="javascript:void(0);" data-category="TestValue">Value 1</a></li> 
<li><a id= .... 

我想要的是在Jquery中獲得數據類別屬性的準確錨,但我現在還不能做到。

我在開發者工具的控制檯

$('#categories > li').children() 

嘗試這樣做,它給了我所有的李德全錨元素,但後來我想選擇只有一個我感興趣的

當我執行以下行時,控制檯返回方括號。幫幫我!!

$('#categories > li').children().find('a[data-category=TestValue]') 
+0

'$('#categories> li')。find('a [data-category =「TestValue」]') – juvian

+0

@juvian,謝謝你的回答。完成,沒有任何反應 –

回答

1

使用$('#categories > li').find('a[data-category="TestValue"]')

小提琴:http://jsfiddle.net/juvian/VDAgS/

+0

好的。對不起,我沒有看到你刪除了兒童功能。我認爲這是關於引號。它的工作原理,謝謝。 –

1

檢查http://jsfiddle.net/yvanavermaet/98K2G/1/

HTML:

<ul id="categories"> 
    <li><a id="cphContents_repeater_linkbutton_0" href="javascript:void(0);" data-category="TestValue">Value 1</a></li> 
    <li><a id="cphContents_repeater_linkbutton_1" href="javascript:void(0);">Value 2</a></li> 
    <li><a id="cphContents_repeater_linkbutton_2" href="javascript:void(0);">Value 3</a></li> 
</ul> 

JS:

var a = $("#categories > li > a[data-category='TestValue']"); 
alert(a.length); // this will alert "1" 
相關問題