如何使用jQuery使用data-filters属性检索“Result One”?

时间:2015-08-19 09:05:09

标签: javascript jquery

如何使用jQuery使用data-filters属性检索“Result One A”和“Result One B”?我想检索具有“[”FilterOne“]”作为数据过滤器的每个结果。

<ul class="list">
    <li data-filters="["FilterOne"]">
        <div class="wrapper">
            <span class="target">Result One A</span>
        </div>
    </li>
    <li data-filters="["FilterOne"]">
        <div class="wrapper">
            <span class="target">Result One B</span>
        </div>
    </li>
    <li data-filters="["FilterTwo"]">
        <div class="wrapper">
            <span class="target">Result Two A</span>
        </div>
    </li>
    <li data-filters="["FilterThree"]">
        <div class="wrapper">
            <span class="target">Result Three A</span>
        </div>
    </li>
</ul>

1 个答案:

答案 0 :(得分:1)

它正常工作,但你的HTML标签有点没格式化(现在只需稍加修改)

$(function() {

  var lis = $('li[data-filters="\\[\\"FilterOne\\"\\]"] > div > span.target').each(function(index) {
    alert($(this).text());
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul class="list">
  <li data-filters='["FilterOne"]'>
    <div class="wrapper">
      <span class="target">Result One A</span>
    </div>
  </li>
  <li data-filters='["FilterOne"]'>
    <div class="wrapper">
      <span class="target">Result One B</span>
    </div>
  </li>
  <li data-filters="[" FilterTwo "]">
    <div class="wrapper">
      <span class="target">Result Two A</span>
    </div>
  </li>
  <li data-filters="[" FilterThree "]">
    <div class="wrapper">
      <span class="target">Result Three A</span>
    </div>
  </li>
</ul>