我尝试获取具有特定类别或没有类别或没有两个类别的项目的文本。
这是我的 html 代码 -
<div id="events">
<ul class="checkout-bar">
<li class=""><span><a href="#">Order</a></span></li>
<li class=""><span><a href="#">Booking</a></span></li>
<li class="t-completed"><span><a href="#">Receiving</a></span></li>
<li class="t-completed"><span><a href="#">Loading</a></span></li>
<li class="t-completed active"><span><a href="#">Arrival</a></span></li>
<li class="inactive"><span><a href="#">Arrival</a></span></li>
<li class="inactive"><span><a href="#">Availability</a></span></li>
<li class="inactive"><span><a href="#">Delivery</a></span></li>
</ul>
</div>
现在,我想获取那些链接标签 () 的文本 li
有't-completed
' 类,哪些有空类,哪些没有 ' t-completed
'和'active
' 类即 <li class="t-completed active">
尝试使用 Jquery -
if($('#events .checkout-bar li').length>0){
$('#events .checkout-bar li').each(function(){
var t = $(this);
if (!t.hasClass() || (t.hasClass("ms-completed") && !t.hasClass("active"))){
//if($(this).is('.ms-completed:not(.active)') || $(this).attr('class')==""){
var oldtxt = $(t).find('span a').text();
if(oldtxt == 'Order'){
t.find('span a').text("Ordered");
}else if(oldtxt == 'Booking'){
t.find('span a').text("Booked");
}else if(oldtxt == 'Receiving'){
t.find('span a').text("Received");
}else if(oldtxt == 'Loading'){
t.find('span a').text("Loaded");
}else if(oldtxt == 'Arrival'){
t.find('span a').text("Arrived");
}else if(oldtxt == 'Availability'){
t.find('span a').text("Available");
}else if(oldtxt == 'Delivery'){
t.find('span a').text("Delivered");
}else if(oldtxt == 'In transit'){
t.find('span a').text("Transited");
}else{
t.find('span a').text(oldtxt);
}
}
});
}
替换那些<a>
标记文本 <li>
有空("")
类和具有 "ms-completed"
仅限类(class)。目前,替换那些 <a>
标记文本 <li>
拥有“ms-completed active”类(class)。
建议?
最佳答案
您可以在这里尝试hasClass()
:
var t = $(this);
if (t.attr("class") === '' || (t.hasClass("t-completed") && !t.hasClass("active"))) {....}
更新:我不知道您的实际代码中是否有这一行:
var oldtxt = $(t).find('span a').text();
但 t 已经等于 $(this)
,所以不要再次将 t 包裹在 $()
中。
此外,这对于 oldtxt
来说应该足够了(基于上面显示的 HTML):
var oldtxt = t.text();
关于javascript - 检查多个类(class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39667221/