javascript 使用 jquery 删除具有 id 的类或元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15871092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
removing class or element with id with jquery
提问by plalx
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(最好完全阻止它加载),但现在我决定在页面加载后删除它。
i tried out these lines of codes here on jsfiddle and it works. and the '#content_gallery' div gets removed.
我在 jsfiddle 上尝试了这些代码行并且它有效。并且“#content_gallery” div 被删除。
<script type='text/javascript'>
$(document).ready(function(){$('#content_gallery').remove();});
</script>
but when i apply it on the head of my test website. it doesnt remove the div with the id specified. i also tried removing other divs with other id labels
但是当我将它应用到我的测试网站的头部时。它不会删除指定 id 的 div。我还尝试删除带有其他 id 标签的其他 div
test site: http://stage.bravo-company.info/luchatest/category/gallery/
测试站点:http: //stage.bravo-company.info/luchatest/category/gallery/
i also tried:
我也试过:
$(document).ready(function(){
$('#content').remove();
});
and
和
$(function(){
$('#content_gallery').remove();
});
any ideas? could it be a case of jquery conflict? im not sure how to confirm this.
有任何想法吗?这可能是jquery冲突的情况吗?我不知道如何确认这一点。
here is the test site: http://stage.bravo-company.info/luchatest/category/gallery/
这里是测试站点:http: //stage.bravo-company.info/luchatest/category/gallery/
回答by plalx
There is another JavaScript library that is conflicting with jQuery
on your site. The correct way of fixing this issue would be to use jQuery.noConflict
.
jQuery
您的网站上有另一个与此冲突的 JavaScript 库。解决此问题的正确方法是使用jQuery.noConflict
.
<script type="text/javascript" src="other_lib.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$.noConflict();
jQuery(document).ready(function($) {
// Code that uses jQuery's $ can follow here.
$(function(){
$('#content_gallery').remove();
});
});
// Code that uses other library's $ can follow here.
</script>
回答by Arun P Johny
It is a case conflict
这是一个案例冲突
use
利用
jquery(document).ready(function(){jquery('#content_gallery').remove();});