如何为包含所选文本的li提供ID?

时间:2012-12-23 01:37:34

标签: jquery

我有这个名单

<ol><li class="first"><a href="/ivory/node/183">Sour Candy Spray 8.5ml (3 flavors)</a></li>
<li><a href="/ivory/node/181">Chap Ice Mini Stick 3g (Cherry)</a></li>
<li><a href="/ivory/node/180">Chap Ice Mini Stick 3g (Kiwi)</a></li>
<li><a href="/ivory/node/179">Chap Ice Mini Stick 3g (Orange)</a></li>
<li><a class="active" href="/ivory/node/166">Chap Ice Beeswax</a></li>
<li><a href="/ivory/node/165">Chap Ice Cherry Stick</a></li>
<li><a href="/ivory/node/170">Chap Ice Crazy Flavors 4.2g (Blueraspberry)</a></li>
<li><a href="/ivory/node/169">Chap Ice Crazy Flavors 4.2g (Watermelon)</a></li>
<li><a href="/ivory/node/168">Chap Ice Crazy Flavors Verry Berry 4.2g</a></li>
<li><a href="/ivory/node/167">Chap Ice Moisture 4.2g Stick</a></li>
<li><a href="/ivory/node/164">Chap Ice Original 4.2g Stick</a></li>
<li class="last"><a href="/ivory/node/163">ChapIce 10G Petrolium Jelly Squeeze Tube</a></li>
</ol>

如何找到包含“Chap Ice Cherry Stick”的li并给它一个唯一的ID?

1 个答案:

答案 0 :(得分:2)

试试这个,

$('li a:contains("Chap Ice Cherry Stick")').parent().attr('id', youruniqueID)

如果您希望出现多个元素

$('li a:contains("Chap Ice Cherry Stick")').each(function(index, elem){
   $(this).parent().attr('id', "chap" + index);
})