1

以下のように 2 つの div 要素があります。

  <div class="sc-info" tags="abc mno">
     ....
  </div>

  <div class="sc-info" tags="abc xyz">
     ....
  </div>

タグに存在する値に基づいて hide() と show() を実行したい.以下のようなもの

  $('div.sc-info[tags with "abc"]').hide()   // This should hide both divs

  $('div.sc-info[tags with "xyz"]').show()   //This should show only second one
4

1 に答える 1

7

属性に単語を含むセレクタ( ~=) セレクタを使用する

http://api.jquery.com/attribute-contains-word-selector/

$('div.sc-info[tags~="abc"]').hide()

$('div.sc-info[tags~="xyz"]').show()

コメントへの返信:

複数の単語 (つまり) str = "New York"; をチェックしているときに機能しません。[str ~="abc New York"] では正しい結果が得られません。それを行う他の方法はありますか?

これらの属性内で複数の単語を選択するにdiv.sc.infoは、可能な両方の属性セレクターをフィルター処理する必要があります。

$('div.sc-info').filter('[tags~="abc"],[tags~="New York"]').hide()

例: http://jsfiddle.net/hunter/AS6Zp/

于 2012-08-29T13:09:32.510 に答える