如何在某个div之后删除div中的所有元素 [英] How to remove all elements in a div after a certain div

查看:346
本文介绍了如何在某个div之后删除div中的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个div,该div在其底部绘制了动态元素,并且无论这些ID使用的是JavaScript/jQuery还是什么,我都想隐藏这些元素.基本上我的HTML看起来像这样:

So I have a div that is drawing in dynamic elements at its bottom and I want to hide these elements, no matter what their IDs are using javaScript/jQuery. Basically my HTML looks like this:

<div class="right-panel">
  <div class="info">Text</div>
  <form id="the-form">
    <input type="hidden" name="first-name" value="">
    <input type="hidden" name="last-name" value="">
    <input type="hidden" name="state" value="">
  </form>
  <script>javaScript</script>
  <div id="dynamic-id-1">Advertisement 1</div>
  <div id="dynamic-id-2">Advertisement 2</div>
</div>

我想确保始终删除或隐藏"dynamic-id-1"和"dynamic-id-2" div,无论它们的ID是什么(它们的ID都可能发生变化).如何定位这些元素而不定位其ID?

I'd like to ensure that the "dynamic-id-1" and "dynamic-id-2" divs are always removed or hidden no matter what their ID's are (their IDs are subject to change). How do I target these elements without targeting their IDs?

编辑-我尝试过,但是我的方法似乎很局限,即使在链接时,我也无法使它与多个div一起使用:

Edit--I tried this, but my approach seems limited, and I couldn't get it to work with multiple divs, even when chaining:

$('#the-form').next().hide();

(注意:不幸的是,它们没有一个类,有多个div,并且ID总是完全不同的.我希望可以有新颖的方法来定位包装div的最后两个div并隐藏它们)

(Note: unfortunately they don't have a class, there are multiple divs, and the IDs are always completely different. I was hoping there might be novel way to target the last two divs of the wrapping div and hide them)

推荐答案

如果script标签始终位于需要删除的div之前,则可以执行此操作-

If the script tag is always before the div's that need removing you could do this -

$('.right-panel > script').nextAll('div').remove();

http://jsfiddle.net/w6d8K/1/

根据您的尝试,您可以执行此操作-

Based on what you tried you could do this -

$('#the-form').nextAll('div').hide();

http://jsfiddle.net/w6d8K/2/

以下是nextAll()的文档- https://api.jquery.com/nextAll/

Here are the docs for nextAll() - https://api.jquery.com/nextAll/

这篇关于如何在某个div之后删除div中的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆