简体   繁体   English

如何在jQuery中搜索一部分属性值?

[英]How to search for a part of attribute value in jquery?

How can I get a part from all data attribute values , count them and change background color of the element . 如何从所有数据属性值中获取零件,对其进行计数更改元素的背景颜色 I have set an example on jsfiddle 我在jsfiddle上设置了一个示例

For example: 例如:

The attribute is called data-search . 该属性称为data-search The value is set to search_12 (the number is to ID the element). 该值设置为search_12 (数字用于标识元素)。

I'm trying to find search_ everywhere in the body (I'd like to target everything within the body and no a specific section) when clicking the button. 单击按钮时,我试图在身体的任何地方找到search_ (我想将身体内的所有物体作为目标,而没有特定的部分)。 So, there is a lot of elements that have the same data attribute , but the difference, is that, the value has a number at the end that changes for identification purpose. 因此,有许多具有相同data属性的元素,但是不同之处在于,该值的末尾有一个数字会出于标识目的而更改。

If it is possible, I'd like to use split method since is faster than match() or RegExp(). 如果可能的话,我想使用split方法,因为它比match()或RegExp()更快。 Otherwise, feel free to answer your way. 否则,请随意回答。

This is what I have. 这就是我所拥有的。

JS JS

$( "button" ).on( "click", function() {

      var search = $( "body" ).children( "*" ).attr( "data-search" )

      var results = search.split( 'search_' ).length - 1;  

      $('.count-results').text( results )

});

HTML HTML

<div class="section">
   <button type="button">Search</button>
</div>

<div class="section">
   <section>
      <div>
         <h1>Please <span class="span" data-search="search_21">find me</span></h1>
         <p class="para">Search for data</p>
      </div>
   </section>

   <article> 
      <section>
         <div>
            <h1>Find the next element's data</h1>
            <p data-search="search_400" class="para">
            Find me too
            </p>
         </div>
      </section>
   </article>

   <details open>
      <address>
        Written by <a href="#" data-search="search_1">Jon Doe</a>.<br> 
        Visit us at:<br>
        Example.com<br>
        Box <span class="number" data-search="search_anything">564</span>, 
        Disneyland<br>
        USA
     </address>
   </details>
</div>

<div class="section">
   <p>The string of <b>search_</b> was found:</p>
   <div>
      <span class="count-results"></span> times
   </div> 
</div>

Any help will be appreciated! 任何帮助将不胜感激!

You can use the Attribute Starts With Selector : 您可以使用“ 以选择器开头属性

 $( "button" ).on( "click", function() { var results = $('[data-search^="search_"]'); results.css('background', 'red'); $('.count-results').text(results.length); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="section"> <button type="button">Search</button> </div> <div class="section"> <section> <div> <h1>Please <span class="span" data-search="search_21">find me</span></h1> <p class="para">Search for data</p> </div> </section> <article> <section> <div> <h1>Find the next element's data</h1> <p data-search="search_400" class="para"> Find me too </p> </div> </section> </article> <details open> <address> Written by <a href="#" data-search="search_1">Jon Doe</a>.<br> Visit us at:<br> Example.com<br> Box <span class="number" data-search="search_anything">564</span>, Disneyland<br> USA </address> </details> </div> <div class="section"> <p>The string of <b>search_</b> was found:</p> <div> <span class="count-results"></span> times </div> </div> 

Just use the appropriate CSS Attribute selector 只需使用适当的CSS属性选择器

[attr^=value]

Represents an element with an attribute name of attr whose value is prefixed (preceded) by value. 表示属性名称为attr的元素,其值以值为前缀(在前面)。

So in your example you would use something along the lines of 因此,在您的示例中,您将使用类似于

$("[data-search^='search_']")

The collection would then hold all elements that had a data-search attribute that was prefixed with search_ 然后,集合将保存所有具有以search_为前缀的data-search属性的search_

Demo 演示

 $("button").click(function(){ var count = $("[data-search^='search_']").length; $(".count-results").text(count); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="section"> <button type="button">Search</button> </div> <div class="section"> <section> <div> <h1>Please <span class="span" data-search="search_21">find me</span></h1> <p class="para">Search for data</p> </div> </section> <article> <section> <div> <h1>Find the next element's data</h1> <p data-search="search_400" class="para"> Find me too </p> </div> </section> </article> <details open> <address> Written by <a href="#" data-search="search_1">Jon Doe</a>.<br> Visit us at:<br> Example.com<br> Box <span class="number" data-search="search_anything">564</span>, Disneyland<br> USA </address> </details> </div> <div class="section"> <p>The string of <b>search_</b> was found:</p> <div> <span class="count-results"></span> times </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM