简体   繁体   English

在页面加载后删除div时jQuery冲突

[英]jQuery conflict when removing div after page load

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. 我正在尝试从页面中删除一个div(最好阻止它加载)但是现在我正在解决页面加载后删除它。

When I try the following lines of code in jsFiddle , the #content div gets removed, as expected. 当我在jsFiddle中尝试以下代码行时 ,会按预期删除#content div。

<script type='text/javascript'>//<![CDATA[ 
  $(window).load(function(){
      $('#content').remove();
  });//]]>  
</script>

However, I have also tried implementing it on an actual website , but in that case, the #content div isn't removed. 但是,我也尝试在实际网站上实现它,但在这种情况下,# #content div不会被删除。

Any suggestions as to what might be wrong? 有什么可能是错的建议吗?

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: 如果您与另一个使用美元进行操作的库共享jQuery,您需要使用匿名包装器来防范它:

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

Note that instead of .load() I'm using .on('load', fn). 请注意,而不是.load()我使用.on('load',fn)。

Instead of on page load you could also bind your code on DOM ready; 您可以将您的代码绑定在DOM上,而不是页面加载; jQuery passes itself as the first argument to the inner function: jQuery将自身作为内部函数的第一个参数传递:

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

Your $ variable does not point to jQuery for some reason. 你的$变量由于某种原因没有指向jQuery。

<script type='text/javascript'>
    window.$ = jQuery;
    $(window).load(function()
    {

        $('#content').remove();
     });
</script>

use this: 用这个:

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

Hope this help. 希望这有帮助。

Try with this 试试这个

<script type='text/javascript'> 
    $(document).ready(function(){
         $('#content').remove();
    });
</script>

or 要么

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

It's because you have a javascript error when you call $(window).load() . 这是因为当你调用$(window).load()时你有一个javascript错误。

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). 此外,您应该更好地使用document.ready,因为内容将被更快地删除(不需要等待加载所有图像)。

//shorthand for $(document).ready()
$(function(){
    $('#content').remove();
});

TypeError: $(...).load is not a function TypeError:$(...)。load不是函数

It is giving error above instead of below one as load is deprecated in new version of Jquery 它在上面给出错误而不是低于1,因为在新版本的Jquery中不推荐使用load

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

use this code 使用此代码

$(function(){
 // your code here
})

You are using jQuery testing with onload, 您正在使用onload进行jQuery测试,

so you have to add onload syntax in jquery, on your site the statement was not called onload, that why its not working 所以你必须在jquery中添加onload语法,在你的站点上语句没有被称为onload,这就是为什么它不起作用

I have updated fiddle 我已经更新了小提琴

http://jsfiddle.net/MarmeeK/FRYsJ/3/

The JS code under <script> tag, without adding in onload in page, <script>标记下的JS代码,没有在页面中添加onload,

<script type="text/javascript">
    $(document).ready(function(){$('#content').remove();});
</script>

this should work :) 这应该工作:)

Case of jquery conflict. jquery冲突的案例。 You have mootools framework too on the page. 你在页面上也有mootools框架。

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();

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

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