简体   繁体   中英

How to check if an element with .Attr has a particular class?

This is the HTML structure:

<div data-step="1"></div>
<div data-step"2" class="active"></div>

Something along those lines (the following is obviously wrong, it's just to provide an idea)

if($("div").attr("data-step", "2").hasClass("active")) {
  //do this...
}

Use attribute equals selector to get the element with a certain attribute value. With attr() method it simply sets the attribute value and returns the jQuery element object.

if($("div[data-step='2']").hasClass("active"))

// or simply combine the class with selector and
// check existance simply by checking its length
if($("div[data-step='2'].active").length)

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