我试图在我的jQuery中添加一个条件。我的剧本是:
$("#mycontent a.half.first"+$id).removeClass("half").prependClass("paid");
$("#mycontent2 a.half.second"+$id).removeClass("half").prependClass("paid");我在桌子上有一个链接:
<table class='grav-results-table' id='mycontent' width='75%'>
<tr class="grav-results-tr">
<td class="grav-results-td">
<a href="#inbound" class="half first3547" onclick="comtype(3547,'inbound', 704)">$184.20</a>我正试图瞄准上面显示的链接。我尝试了以下几种不同的变体:
if ( $( "#mycontent" ).hasClass( "half first" ) ) {
$("#mycontent a.half.first"+$id).removeClass("half").prependClass("paid");
$("#mycontent2 a.half.second"+$id).removeClass("half").prependClass("paid");
}我也尝试过:
if ( $( this ).hasClass( "half first" ) )这段代码位于AJAX脚本的成功区域。
但我似乎找不到正确的方法来瞄准全班。我做错了什么?
发布于 2018-05-25 00:27:51
如果条件需要在表的子表中查找类,则需要更改.就像这样:
if ($("#mycontent").find("a.half").length > 0) {
console.log('children found');
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class='grav-results-table' id='mycontent' width='75%'>
<tr class="grav-results-tr">
<td class="grav-results-td">
<a href="#inbound" class="half first3547"></a>
</td>
</tr>
</table>
发布于 2018-05-25 00:35:28
加上马丁,你可以试试这个。
if ( $( "table#mycontent" ).children().hasClass( "half first" ) ) {
alert("I have class in it.");
} https://stackoverflow.com/questions/50519807
复制相似问题