简体   繁体   English

无法使用jQuery的remove()删除元素

[英]Can't remove element with jquery's remove()

I'm making a greasemonkey script for this website: http://tinyurl.com/websiteasdasd and I want to remove the element rodape-ao-vivo , problem is that doing $("#rodape-ao-vivo").remove(); 我正在为此网站制作一个润滑脂脚本: http : rodape-ao-vivo ,我想删除元素rodape-ao-vivo ,问题是执行$("#rodape-ao-vivo").remove(); is not working, however, if I do something like 不起作用,但是,如果我做类似的事情

setInterval(function() {
        $("#rodape-ao-vivo").remove();
    }, 1000);

it works just fine, so I'm basically asking you to help me understand what's going on here. 它工作正常,所以我基本上是在请您帮助我了解这里的情况。 Maybe it's just a small stupid thing but I'm sleepless and really want to get this done. 也许这只是个小愚蠢的事情,但是我不眠不休,真的很想完成这项工作。

This DIV is dynamically generated by script, to remove it, you need to wait until this element is availabe in DOM, eg if for some reason other answers don't work: {or use DOM ready handler} 这个DIV是由脚本动态生成的,要删除它,您需要等到DOM中可以使用此元素,例如,如果由于某些原因其他答案不起作用:{或使用DOM ready处理程序}

$(window).on('load', function () {
    var interval = setInterval(function () {
        if ($("#rodape-ao-vivo").length) {
            $("#rodape-ao-vivo").remove();
            clearInterval(interval);
        }
    }, 100);
});

Hmm, perhaps you are trying to remove the element before it's created? 嗯,也许您正在尝试在元素创建之前将其删除? (The delay helps because in that time the element gets created and you can remove it) (延迟会有所帮助,因为在那个时候创建​​了元素,您可以将其删除)

You probably try to remove the element before it even exists. 您可能试图在元素存在之前就将其删除。 Adding one second delay gives the browser enough time to render the element, so you can remove it then. 添加一秒钟的延迟会给浏览器足够的时间来呈现元素,因此您可以将其删除。 But it's not guaranteed to work, because one second may not be enough to render that element (slow connection, for example). 但这不能保证能正常工作,因为一秒钟可能不足以呈现该元素(例如,慢速连接)。

So you should make sure the document is ready, jQuery's way to do this is: 因此,您应确保文档已准备就绪,jQuery的方法是:

$(function() {
    $("#rodape-ao-vivo").remove();
});

Or you can wait for the whole document to load: 或者,您可以等待整个文档加载:

window.onload = function() {
    $("#rodape-ao-vivo").remove();
}

如果您在页面加载时执行此操作,则使用document.ready();包装代码。

$(document).ready(function(){$("#rodape-ao-vivo").remove();});

Please make sure that all the elements gets loaded before your use them.So wrap your code inside function which will ensure your elements has been attached to DOM 使用前请确保所有元素都已加载。因此将代码包装在函数中,这将确保元素已附加到DOM

$("#rodape-ao-vivo").remove(); //will probably will give you an error.

$(function() {
    $("#rodape-ao-vivo").remove();
}

Or , 要么 ,

$( document ).ready(function() {
    $("#rodape-ao-vivo").remove();
})

Or, 要么,

jQuery( document ).ready(function( $ ) {
    $("#rodape-ao-vivo").remove();
});

Or, 要么,

window.onload = function() {
    $("#rodape-ao-vivo").remove();
}

But when you do , 但是当你这样做时

setInterval(function() {
        $("#rodape-ao-vivo").remove();
}, 1000);

This will give an element time to get load in your DOM .So It working fine.But instead of using setInterval you can use function listed above 这将为元素提供时间来获取DOM负载。因此它可以正常工作。但是可以使用上面列出的函数来代替setInterval

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM