Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

javascript - Jquery search and display images based on class

I'd like to be able to search and display these based on the class attributes, such as "kitchen". I can't find anything on searching by the class, only text and such. Or if anyone has a recommendation on a better way to search and display images. Any help would be greatly appreciated.

<form id="live-search" method="post">
  <fieldset>
    <input type="text" class="text-input" id="filter" />
    <span id="filter-count"></span>
  </fieldset>
</form>

<br/>
<br/>

<div id="gallery">
  <div id="item" class="#1 Category-Home Home">
    <a id="#image-link" target="_blank" a href="">
      <img class="img_item"><img src="http://placehold.it/150x150" />

    </a>
  </div>
  <div id="item" class="#2 Category-Kitchen Kitchen">
    <a id="#image-link" target="_blank" a href="">
      <img class="img_item"><img src="http://placehold.it/150x150" />

    </a>
  </div>
  <div id="item">
    <div class="#3 Category-Outdoors Outdoors">
      <a id="#image-link" target="_blank" a href="">
        <img class="img_item"><img src="http://placehold.it/150x150" />

      </a>
    </div>

  </div>
  <div id="item" class="#4 Category-Sports Sports">
    <a id="#image-link" target="_blank" a href="">
      <img class="img_item"><img src="http://placehold.it/150x150" />

    </a>
  </div>
</div>

$(document).ready(function() {
  $("#filter").keyup(function() {


  });
});

https://jsfiddle.net/sgrg4b06/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So first, thing, remove all the duplicate ID's all over the place. That will only cause headaches. ID's are unique identifiers. Second, see the below. Pretty well commented. It IS case sensitive.

// then, on entering text...
  $("#filter").on("keyup", function() {
  if ($(this).val().length > 0) {
    // hide everything,
    $(".item").hide();
    // get the text in the input
    var mySelector = $(this).val();
    // show any class that contains the input
    var myImgs = $("[class*='"+mySelector+"' i]");
    myImgs.show();
  } else {
    $(".item").show();
  }

  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="live-search" method="post">
  <fieldset>
    <input type="text" class="text-input" id="filter" />
    <span id="filter-count"></span>
  </fieldset>
</form>

<br/>
<br/>

<div id="gallery">
  <div id="image-1" class="item #1 Category-Home Home">
    <a target="_blank" a href="">
      <img class="img_item"><img src="http://placehold.it/150x150" />

    </a>
  </div>
  <div id="image-2" class="item #2 Category-Kitchen Kitchen">
    <a target="_blank" a href="">
      <img class="img_item"><img src="http://placehold.it/150x150" />

    </a>
  </div>
  <div id="image-3" class="item #3 Category-Outdoors Outdoors">
      <a target="_blank" a href="">
        <img class="img_item"><img src="http://placehold.it/150x150" />

      </a>
    </div>

  </div>
  <div id="image-4" class="item #4 Category-Sports Sports">
    <a target="_blank" a href="">
      <img class="img_item"><img src="http://placehold.it/150x150" />

    </a>
  </div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...