.foo を選択する必要がありますが、.foo を含むタグが 2 つあり、.foo.bar を含むタグではなく、.foo のみを含むタグを選択する必要があります。どうすればこれを達成できますか?
<a class="foo bar">text</a>
<a class="foo">text</a>
.foo を見つけるために使用しているコードは
$(this).parents('.post').find('.foo').text('text');
.foo を選択する必要がありますが、.foo を含むタグが 2 つあり、.foo.bar を含むタグではなく、.foo のみを含むタグを選択する必要があります。どうすればこれを達成できますか?
<a class="foo bar">text</a>
<a class="foo">text</a>
.foo を見つけるために使用しているコードは
$(this).parents('.post').find('.foo').text('text');
jQuery .not(); を使用します。http://api.jquery.com/not/
$(this).parents('.post').find('.foo').not('.bar').text('text');
いくつかの変更
.bar
要素を除外します試す
$(this).closest('.post').find('.foo:not(.bar)').text('text');
試す
$(this).parents('.post').find('.foo').not('.bar').text('text');
クラス属性で選択するだけです:
.find('[class=foo]')
あなたがしたい:not(selector)
:
$(this).parents('.post').find('.foo:not(.bar)').text('text');
http://api.jquery.com/not-selector/を参照してください
.not() メソッドを使用する必要があります。
$(this).parents('.post').find('.foo').not('.bar').text('text');
ここで詳細を読むことができます: http://api.jquery.com/not-selector/