Jquery-获取当前选择器的Class属性

Jquery-获取当前选择器的Class属性,jquery,Jquery,锚定标记具有onclick事件: <a href="javascript:void(0);" class="Gicon Favor" advertid="@item.adID" onclick="followadv('@item.adID'); return false;"></a> followad函数在许多标记中调用,其中一些标记具有class='Gicon-followad'。我无法获取调用元素的类值$(this).attr('class')不起作用。解决方案1

锚定标记具有onclick事件:

<a href="javascript:void(0);" class="Gicon Favor" advertid="@item.adID" 
onclick="followadv('@item.adID'); return false;"></a>

followad
函数在许多
标记中调用,其中一些标记具有
class='Gicon-followad'
。我无法获取调用元素的类值<代码>$(this).attr('class')不起作用。

解决方案1:您需要使用名称值选择器,因为您尚未传递已单击元素对象的引用:

$('[advertid="'+advertid+'"]').attr('class')
解决方案2:要将其用于此,您需要将onlclick事件修改为
onclick=“followadv(this)”


解决方案1:您需要使用名称值选择器,因为您尚未传递已单击元素对象的引用:

$('[advertid="'+advertid+'"]').attr('class')
解决方案2:要将其用于此,您需要将onlclick事件修改为
onclick=“followadv(this)”


解决方案1:您需要使用名称值选择器,因为您尚未传递已单击元素对象的引用:

$('[advertid="'+advertid+'"]').attr('class')
解决方案2:要将其用于此,您需要将onlclick事件修改为
onclick=“followadv(this)”


解决方案1:您需要使用名称值选择器,因为您尚未传递已单击元素对象的引用:

$('[advertid="'+advertid+'"]').attr('class')
解决方案2:要将其用于此,您需要将onlclick事件修改为
onclick=“followadv(this)”

Html:

<a href="javascript:void(0);" class="Gicon Favor" advertid="@item.adID" onclick="followadv(this); return false;">Link</a>
function followadv(advertid) {
    if ($(advertid).attr('class')=="Gicon Favor") {
       alert("Active");
    }
    else{
       alert("Inactive");
    }
}

对于您的代码

function followadv(advertid) {
    if ($('[advertid="'+advertid+'"]').attr('class')=="Gicon Favor") {
       alert("Active");
    }
    else{
       alert("Inactive");
    }
}
Html:

<a href="javascript:void(0);" class="Gicon Favor" advertid="@item.adID" onclick="followadv(this); return false;">Link</a>
function followadv(advertid) {
    if ($(advertid).attr('class')=="Gicon Favor") {
       alert("Active");
    }
    else{
       alert("Inactive");
    }
}

对于您的代码

function followadv(advertid) {
    if ($('[advertid="'+advertid+'"]').attr('class')=="Gicon Favor") {
       alert("Active");
    }
    else{
       alert("Inactive");
    }
}
Html:

<a href="javascript:void(0);" class="Gicon Favor" advertid="@item.adID" onclick="followadv(this); return false;">Link</a>
function followadv(advertid) {
    if ($(advertid).attr('class')=="Gicon Favor") {
       alert("Active");
    }
    else{
       alert("Inactive");
    }
}

对于您的代码

function followadv(advertid) {
    if ($('[advertid="'+advertid+'"]').attr('class')=="Gicon Favor") {
       alert("Active");
    }
    else{
       alert("Inactive");
    }
}
Html:

<a href="javascript:void(0);" class="Gicon Favor" advertid="@item.adID" onclick="followadv(this); return false;">Link</a>
function followadv(advertid) {
    if ($(advertid).attr('class')=="Gicon Favor") {
       alert("Active");
    }
    else{
       alert("Inactive");
    }
}

对于您的代码

function followadv(advertid) {
    if ($('[advertid="'+advertid+'"]').attr('class')=="Gicon Favor") {
       alert("Active");
    }
    else{
       alert("Inactive");
    }
}

你为什么要把普通的JS和jQuery混合在一起

如果您已经知道要在哪些元素上进行单击事件(基于类名),那么为什么不选择以下内容:

HTML

<a href="#" class="Gicon Favor" advertid="@item.adID"></a>

另外,我可能会将HTML标记更改为
dataadvertid=
,而不是
advertid=
,因为它与HTML5约定保持一致。

为什么要将vanilla JS与jQuery混合使用

如果您已经知道要在哪些元素上进行单击事件(基于类名),那么为什么不选择以下内容:

HTML

<a href="#" class="Gicon Favor" advertid="@item.adID"></a>

另外,我可能会将HTML标记更改为
dataadvertid=
,而不是
advertid=
,因为它与HTML5约定保持一致。

为什么要将vanilla JS与jQuery混合使用

如果您已经知道要在哪些元素上进行单击事件(基于类名),那么为什么不选择以下内容:

HTML

<a href="#" class="Gicon Favor" advertid="@item.adID"></a>

另外,我可能会将HTML标记更改为
dataadvertid=
,而不是
advertid=
,因为它与HTML5约定保持一致。

为什么要将vanilla JS与jQuery混合使用

如果您已经知道要在哪些元素上进行单击事件(基于类名),那么为什么不选择以下内容:

HTML

<a href="#" class="Gicon Favor" advertid="@item.adID"></a>

另外,我可能会将HTML标记更改为
dataadvertid=
,而不是
advertid=
,因为它与HTML5约定保持一致。

语法错误。最后一行
})。语法错误。最后一行
})。语法错误。最后一行
})。语法错误。最后一行
})。它与onclick=“followadv('@item.adID')”和onclick=“followadv(this)”一起工作。它与onclick=“followadv('@item.adID')”和onclick=“followadv(this)”一起工作。它与onclick=“followadv('@item.adID')”和onclick=“followadv(this)”一起工作。它与onclick=“followadv('@item.adID')”和onclick=“followadv(this)”一起工作(本)"。