2

liが「Thing」と言っている場合は追加したいのですが、「Things」と言っている場合は追加しません。それは可能:containsですか?

$("ul li:nth-child(2):contains('Thing')").append("foo");
4

3 に答える 3

7

not関数があります:

$("ul li:nth-child(2):contains('Thing')").not(':contains("Things")').append("foo");
于 2012-10-02T18:56:42.173 に答える
3

Thing要素にテキストコンテンツが正確に含まれている場合に要素を選択する場合は、次のfilter方法を使用できます。

$("ul li:nth-child(2)").filter(function(){
       return $(this).text().trim() === 'Thing'
}).append("foo");
于 2012-10-02T18:57:50.770 に答える
0

コードを少し圧縮するのに役立つ:not()セレクターもあります。

だからそれは

$("ul li:nth-child(2):contains('Thing'):not(:contains('Things'))").append("foo");

フィドルの小さな例。

于 2013-12-04T15:16:40.517 に答える