简体   繁体   中英

how to use jquery.ON() just on elements with specific data-attribute ?

How can I use jquery.on event just on elements that have specific data-attribute ?

for example :

$(document).on( "click", "ELEMENTS WITH DATA-GOOD", function() {
      console.log("good");
});

Thanks

By using has attribute selector :

$(document).on( "click", "[attribute]", function() {
    console.log("good");
});

Try this.,

$(document).ready(function(){
    $('*[data-attribute="ELEMENTS WITH DATA-GOOD"]').on( "click", function() {
          console.log("good");
    });
 });

This will work:

$(document).on( "click", "[attr]", function() {
    console.log("working");
});

For more CSS attribute selectors see https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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