jQuery conflict when removing div after page load

后端 未结 8 457
北海茫月
北海茫月 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:38

    Your $ variable does not point to jQuery for some reason.

    <script type='text/javascript'>
        window.$ = jQuery;
        $(window).load(function()
        {
    
            $('#content').remove();
         });
    </script>
    
    0 讨论(0)
  • 2021-01-13 07:39

    Case of jquery conflict. You have mootools framework too on the page.

    Also I did a 'view source' of the page and I found out this line

    $j = jQuery.noConflict();  //line 132
    

    So try this

    $j('#content').remove();
    Or
    jQuery('#content').remove();
    
    0 讨论(0)
  • 2021-01-13 07:50

    It's because you have a javascript error when you call $(window).load().

    Uncaught TypeError: Object [object global] has no method 'load'
    

    Also, you should better use document.ready instead as the content will be removed faster (doesn't need to wait for all the images to load).

    //shorthand for $(document).ready()
    $(function(){
        $('#content').remove();
    });
    
    0 讨论(0)
  • 2021-01-13 07:51

    use this:

    <script type="text/javascript">
    var node = document.getElementById('content'); 
    if(node.parentNode)
    { 
    node.parentNode.removeChild(node);
    }
    </script>
    

    Hope this help.

    0 讨论(0)
  • 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();
    });
    
    0 讨论(0)
  • 2021-01-13 07:59

    TypeError: $(...).load is not a function

    It is giving error above instead of below one as load is deprecated in new version of Jquery

    $(window).load(function(){
    });
    

    use this code

    $(function(){
     // your code here
    })
    
    0 讨论(0)
提交回复
热议问题