如何检查是否已单击特定链接(JS)



我正在制作一个Chrome扩展程序,我想做的是在Facebook上点击了特定链接时提醒用户。在我的脚本中,每次我单击链接时,它总是提醒访问 gma。

$(document).on("click", "a", function() {
//this == the link that was clicked
var href = $(this).attr("href");
if (window.location.protocol == 'https:'){
if(href == "gmanetwork"){
alert("Accessing Gma");
}}
else{
alert("false");
}
});

我会建议这样的事情:

您将 html 链接更改为 :

<a href="http://yourlink" targetLink="GMA Network"> Your link </a>

和您的脚本:

$(document).on("click", "a", function() {
if($(this).attr("targetLink"){
alert("You are going to the following page: " + $(this).attr("targetLink"));
}
});

Code at Question使用运算符=将值if (window.location.protocol = 'https:'){"gmanetwork"分配给href

if (href = "gmanetwork")

不要检查值是否相等,而是使用===运算符

if (window.location.protocol === 'https:') {
if (href === "gmanetwork") {
alert("Accessing Gma");
}
}

最新更新