2017-03-26 96 views
1

這是HTML結構:如何檢查具有.Attr的元素是否具有特定的類?

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

沿着這些東西線(以下顯然是錯誤的,它只是提供一種思路)

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

的'$(「格」)。ATTR(「數據步」,「2」)'設置一個值 – LGSon

+0

@LGSon我知道是錯的,只是一個口頭上更例如嘗試更好地解釋我在找什麼 –

回答

3

使用attribute equals selector有特定屬性來獲取元素值。使用attr()方法,它只需設置屬性值並返回jQuery元素對象。

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) 
+1

@ rob.m:很高興幫助:) –

相關問題