Javascript 如何检查链接中是否存在类?
我试图检查一个条件,如果链接包含当前类,那么我需要执行一些操作。链接具有自定义属性数据步骤Javascript 如何检查链接中是否存在类?,javascript,Javascript,我试图检查一个条件,如果链接包含当前类,那么我需要执行一些操作。链接具有自定义属性数据步骤 <a href="#" data-step="1" class="multistep_section">login</a> <a href="#" data-step="2" class="multistep_section">billing address</a> <a href="#" data-step="3" class="current mu
<a href="#" data-step="1" class="multistep_section">login</a>
<a href="#" data-step="2" class="multistep_section">billing address</a>
<a href="#" data-step="3" class="current multistep_section">Shipping Address</a>
<a href="#" data-step="4" class="multistep_section">payment</a>
<a href="#" data-step="5" class="multistep_section">order review</a>
当data step=3将包含当前类时,我需要执行一些操作
<a href="#" data-step="1" class="multistep_section">login</a>
<a href="#" data-step="2" class="multistep_section">billing address</a>
<a href="#" data-step="3" class="current multistep_section">Shipping Address</a>
<a href="#" data-step="4" class="multistep_section">payment</a>
<a href="#" data-step="5" class="multistep_section">order review</a>
链接如下:
<a href="#" data-step="1" class="multistep_section">login</a>
<a href="#" data-step="2" class="multistep_section">billing address</a>
<a href="#" data-step="3" class="current multistep_section">Shipping Address</a>
<a href="#" data-step="4" class="multistep_section">payment</a>
<a href="#" data-step="5" class="multistep_section">order review</a>
类电流是可变的,并根据步骤进行更改。只有当data step=3包含当前类时,我才需要在jquery或javascript中执行一些操作
<a href="#" data-step="1" class="multistep_section">login</a>
<a href="#" data-step="2" class="multistep_section">billing address</a>
<a href="#" data-step="3" class="current multistep_section">Shipping Address</a>
<a href="#" data-step="4" class="multistep_section">payment</a>
<a href="#" data-step="5" class="multistep_section">order review</a>
欢迎任何建议根据海报反馈编辑回复:
<a href="#" data-step="1" class="multistep_section">login</a>
<a href="#" data-step="2" class="multistep_section">billing address</a>
<a href="#" data-step="3" class="current multistep_section">Shipping Address</a>
<a href="#" data-step="4" class="multistep_section">payment</a>
<a href="#" data-step="5" class="multistep_section">order review</a>
$(document).ready(function(){
if ($('a[data-step=3]').hasClass('current')){
// Current class exists on data-step 3
}
});
对于纯JS:
<a href="#" data-step="1" class="multistep_section">login</a>
<a href="#" data-step="2" class="multistep_section">billing address</a>
<a href="#" data-step="3" class="current multistep_section">Shipping Address</a>
<a href="#" data-step="4" class="multistep_section">payment</a>
<a href="#" data-step="5" class="multistep_section">order review</a>
(function(){
var elements = document.querySelectorAll('[data-step]');
var targetElement;
for (var i = 0; i < elements.length; i++){
if (elements[i].getAttribute('data-step') == 3){
targetElement = elements[i];
}
}
if (targetElement.classList.contains('current')){
alert("Has current class");
}
})();
参见普通JS示例:
<a href="#" data-step="1" class="multistep_section">login</a>
<a href="#" data-step="2" class="multistep_section">billing address</a>
<a href="#" data-step="3" class="current multistep_section">Shipping Address</a>
<a href="#" data-step="4" class="multistep_section">payment</a>
<a href="#" data-step="5" class="multistep_section">order review</a>
document.AddEventListenerDomainContentLoaded,functionevent{
var links=document.getElementsByTagNamea;
var class_name=当前;
//如果元素包含在数组函数中
函数isInArrayvalue,数组{
返回array.indexOfvalue>-1;
}
forvar i=0;i