jQuery 删除包含另一个具有 ___ 类的元素的元素

发布于 2025-01-03 15:47:12 字数 1232 浏览 1 评论 0原文

我已经找了好几天了,但找不到解决这个问题的方法。这可能非常明显,但是有没有办法删除包含具有某类的另一个元素的元素?在本例中,我想删除包含“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终难愈 2025-01-10 15:47:12

...有没有办法删除包含 tumblr_blog 类锚点的段落?

您可以使用 :has() 选择器。

$('p:has(a.tumblr_blog)').remove();

jsFiddle

为了获得更好的性能,请选择所有 p,然后使用 has() 方法。

如果你没有 jQuery,你可以使用...

var allP = document.querySelectorAll('p');

Array.forEach(allP, function(p) {
    if (p.querySelector('a.tumblr_blog').length) {
        p.parentNode.removeChild(p);
    }
});

所以它可以与 无限滚动插件,根据文档加载新内容时将此代码添加到回调中。

...is there a way to remove a paragraph that contains an anchor with the class of tumblr_blog?

You could use the :has() selector.

$('p:has(a.tumblr_blog)').remove();

jsFiddle.

For better performance, select all p and then use the has() method.

If you don't have jQuery, you could do it with...

var allP = document.querySelectorAll('p');

Array.forEach(allP, function(p) {
    if (p.querySelector('a.tumblr_blog').length) {
        p.parentNode.removeChild(p);
    }
});

So it works with the Infinite Scroll plugin, add this code to the callback when new content is loaded in as per the documentation.

时光是把杀猪刀 2025-01-10 15:47:12

只需首先选择具有特定类的元素,然后删除其父元素。

<p>
 :
 <a class="tumblr_blog">...</a>
 ...
</p>

<script type="text/javascript">
$(document).ready(function() {
  $('p a.tumblr_blog').parent().remove();
});
</script>

Just start by selecting the element with the specific class, then remove its parent.

<p>
 :
 <a class="tumblr_blog">...</a>
 ...
</p>

<script type="text/javascript">
$(document).ready(function() {
  $('p a.tumblr_blog').parent().remove();
});
</script>
流年已逝 2025-01-10 15:47:12

或者您可以执行“普通”CSS 选择器,然后使用 .parent() 方法。

$("p a.tumblr_blog").parent().remove();

Or you could do a 'normal' CSS selector and then use the .parent() method.

$("p a.tumblr_blog").parent().remove();
∞梦里开花 2025-01-10 15:47:12

document.querySelectorAll("*[class~='className']") 将返回具有该类的所有节点的节点列表。然后循环遍历每个节点并使用 .parentNode 获取对其父节点的引用,然后您可以使用 ref.parentNode.removeChild(ref); 将其从其 .parentNode 中删除。把所有的放在一起:

var nodesWithClass = document.querySelectorAll("*[class~='className']");
for (var i=0;i<nodesWithClass.length;i++)
{
    nodesWithClass[i].parentNode.parentNode.removeChild(nodesWithClass[i].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:

var nodesWithClass = document.querySelectorAll("*[class~='className']");
for (var i=0;i<nodesWithClass.length;i++)
{
    nodesWithClass[i].parentNode.parentNode.removeChild(nodesWithClass[i].parentNode);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文