gpt4 book ai didi

javascript - 如果没有类(class),则自动单击按钮

转载 作者:行者123 更新时间:2023-12-03 10:34:21 25 4
gpt4 key购买 nike

如果 a href 没有类 .active,我会尝试触发并自动单击它。我的目标是创建类似 Tom from myspace 的东西,以自动将成员添加为 friend 。但是这段代码似乎忽略了按钮是否已被单击(已经有类.active)并且当我刷新页面时它会再次自动单击。这是我的代码

if ($(".follow-1:not(.active)")) {
jQuery(function(){
jQuery('.follow-1').click(SK_registerFollow(1));
});
} else {
// do nothing
}

我也尝试过,但也没有成功:

if ($('.follow-1').hasClass("active")) {
// do nothing
} else {
jQuery(function(){
jQuery('.follow-1').click(SK_registerFollow(1));
});
}

------------- 已更新 --------------

HTML代码是这样的:

<a class="follow-1" onclick="SK_registerFollow(1);">follow</a>

最佳答案

好吧,这里有很多问题,所以我将内联评论:

// $() will always return a function, so this will always fire
if ($(".follow-1:not(.active)")) {
// This is waiting for a document.ready, so you need to do this on the outside
/// of your code
jQuery(function(){
// This will execute SK_registerFollow immediately
// and send the result to your handler. Probably not what you want
jQuery('.follow-1').click(SK_registerFollow(1));
});
} else {
// No need to ever have a block which 'does nothing'
// do nothing
}

这是一个固定版本:

// Wait for DOM to become ready
$(function() {
// Store a reference to the link, so we don't need to search for it twice
var $follow = $(".follow-1");
// Test to see if the link has the active class
if (!$follow.hasClass("active")) {
$follow.click(); // This will trigger the appropriate onclick handler
}
});

jsFiddle Demo

关于javascript - 如果没有类(class),则自动单击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29072200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com