jQuery conflict when removing div after page load

后端 未结 8 465
北海茫月
北海茫月 2021-01-13 07:07

I\'m trying to remove a div from the page (preferably prevent it from loading at all) but for now I\'m settling on removing it after the page loads.

When I try the f

8条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-13 07:52

    If you're sharing jQuery with another library that uses the dollar for its operation you need to guard against it like this, using an anonymous wrapper:

    (function($) {
        $(window).on('load', function(){
            $('#content').remove();
        });
    }(jQuery));
    

    Note that instead of .load() I'm using .on('load', fn).

    Instead of on page load you could also bind your code on DOM ready; jQuery passes itself as the first argument to the inner function:

    jQuery(function($) {
        $('#content').remove();
    });
    

提交回复
热议问题