jQuery 删除包含另一个具有 ___ 类的元素的元素
我已经找了好几天了,但找不到解决这个问题的方法。这可能非常明显,但是有没有办法删除包含具有某类的另一个元素的元素?在本例中,我想删除包含“tumblr_blog”类锚点的
元素。我会使用 :contains,但是段落默认包含一个没有类的冒号,并且段落元素没有类,那么有没有办法删除包含 tumblr_blog 类的锚点的段落?该脚本还应该删除段落中的 :,即使它没有类。如果有人能帮助我,那我的夜晚真的会很美好。谢谢!
编辑:谢谢亚历克斯!最后一件事,它在第一页上工作,但是当 Paul Irish 通过无限滚动功能加载新元素时,代码不再工作,因为加载新页面时没有刷新。这是我的代码,有没有办法在加载的新元素上实现代码?
<script>
$(function(){
var $container = $('#posts');
$container.imagesLoaded(function(){
$container.masonry({
itemSelector: '#entry',
columnWidth: 370,
});
});
$container.infinitescroll({
navSelector : '#page-nav', // selector for the paged navigation
nextSelector : '#page-nav a', // selector for the NEXT link (to page 2)
itemSelector : '#entry', // selector for all items you'll retrieve
loading: {
finishedMsg: '<em></em>',
}
},
// trigger Masonry as a callback
function( newElements ) {
// hide new items while they are loading
var $newElems = $( newElements ).css({ opacity: 0 });
// ensure that images load before adding to masonry layout
$newElems.imagesLoaded(function(){
// show elems now they're ready
$newElems.animate({ opacity: 1 });
$container.masonry( 'appended', $newElems, true );
});
}
);
});
</script>
I've looked for days and I cannot find a solution to this problem. It's probably VERY obvious, but is there a way to remove an element that contains another element with the class of something? In this case, I want to remove a <p>
element that contains an anchor with the class of "tumblr_blog". I would use :contains, but the paragraph by default contains a colon which does not have a class, and the paragraph element does not have a class, so is there a way to remove a paragraph that contains an anchor with the class of tumblr_blog? This script should also remove the : in the paragraph even though it doesn't have a class. If anyone could help me it would really make my night. Thanks!
EDIT: Thank you Alex! One last thing, it works on the first page, but when new elements are loaded via the infinite-scroll function by Paul Irish, the code no longer works because a new page is loaded without refreshing. Here is my code, is there a way to implement the code on new elements that are loaded?
<script>
$(function(){
var $container = $('#posts');
$container.imagesLoaded(function(){
$container.masonry({
itemSelector: '#entry',
columnWidth: 370,
});
});
$container.infinitescroll({
navSelector : '#page-nav', // selector for the paged navigation
nextSelector : '#page-nav a', // selector for the NEXT link (to page 2)
itemSelector : '#entry', // selector for all items you'll retrieve
loading: {
finishedMsg: '<em></em>',
}
},
// trigger Masonry as a callback
function( newElements ) {
// hide new items while they are loading
var $newElems = $( newElements ).css({ opacity: 0 });
// ensure that images load before adding to masonry layout
$newElems.imagesLoaded(function(){
// show elems now they're ready
$newElems.animate({ opacity: 1 });
$container.masonry( 'appended', $newElems, true );
});
}
);
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
:has()
选择器。jsFiddle。
为了获得更好的性能,请选择所有
p
,然后使用has()
方法。如果你没有 jQuery,你可以使用...
所以它可以与 无限滚动插件,根据文档加载新内容时将此代码添加到回调中。
You could use the
:has()
selector.jsFiddle.
For better performance, select all
p
and then use thehas()
method.If you don't have jQuery, you could do it with...
So it works with the Infinite Scroll plugin, add this code to the callback when new content is loaded in as per the documentation.
只需首先选择具有特定类的元素,然后删除其父元素。
Just start by selecting the element with the specific class, then remove its parent.
或者您可以执行“普通”CSS 选择器,然后使用
.parent()
方法。Or you could do a 'normal' CSS selector and then use the
.parent()
method.document.querySelectorAll("*[class~='className']") 将返回具有该类的所有节点的节点列表。然后循环遍历每个节点并使用 .parentNode 获取对其父节点的引用,然后您可以使用 ref.parentNode.removeChild(ref); 将其从其 .parentNode 中删除。把所有的放在一起:
document.querySelectorAll("*[class~='className']") will return a nodelist of all of the nodes with that class. Then loop through each one and use .parentNode to get a reference to its parent, which you can then remove it from its .parentNode using ref.parentNode.removeChild(ref); Put all together: