JQuery新手,不知道如何使用局部变量(使用greasemonkey)

new to JQuery, dont know how to use local variables (with greasemonkey)

本文关键字:使用 greasemonkey 局部变量 何使用 新手 不知道 JQuery      更新时间:2023-09-26

我试图用Greasemonkey改变按钮的点击事件,在尝试这样做的同时,我遇到了多个问题,并在网上查找了一些东西,但找不到任何我理解或适合我的解决方案。

我要试的是:我试图改变一个网站,它在按下按钮时添加2个元素(我称之为变量"但是"),但是我想删除它添加的第二个元素,所以我覆盖了click事件,所以它添加了两个元素,然后执行了一个函数,删除了第二个元素。

所以BC我是新的,我不太确定如何从jquery得到的东西,并将其设置为本地javascript变量。

问题代码:

var but = $("#information");
but = but.children()[1];
but = but.find( "div" );
but = but.children[0];
but = but.find("a");
but = but.children[0];
$(but).click(function()
{
    if(condition == 'true')
    {
        $.when(initialize()).then(deletehtml());
    }
});
function deletehtml()
{
    $($( "#content_box" ).children()[1]).remove();
}

谢谢你的帮助!

我不知道这是否是你的全部问题,但你的部分问题很可能来自[#]的使用。这打破了一个DOM元素的jQuery对象,之后你不能使用jQuery方法对它,除非你重新包装$()它。相反,使用eq(#)来获得元素,如果你想继续使用jQuery方法对它,并避免不得不重新包装。

var $but = $("#information").children().eq(1);
$but = $but.find( "div" ).first();
$but = $but.find("a").first();
$but.on('click', function() {
    if (condition == 'true') {
        $.when(initialize()).then(deletehtml());
    }
});
function deletehtml() {
    $( "#content_box" ).children().eq(1).remove();
}