[英]How to remove all elements in a div after a certain div
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. 所以我有一个div在底部绘制动态元素,我想隐藏这些元素,无论他们的ID是使用javaScript / jQuery。 Basically my HTML looks like this:
基本上我的HTML看起来像这样:
<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>
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). 我想确保“dynamic-id-1”和“dynamic-id-2”div总是被移除或隐藏,无论他们的ID是什么(他们的ID可能会发生变化)。 How do I target these elements without targeting their IDs?
如何在不定位ID的情况下定位这些元素?
Edit--I tried this, but my approach seems limited, and I couldn't get it to work with multiple divs, even when chaining: 编辑 - 我尝试了这个,但我的方法似乎有限,我无法使用多个div,即使链接:
$('#the-form').next().hide();
(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) (注意:不幸的是,他们没有一个类,有多个div,ID总是完全不同。我希望可能有新颖的方法来定位包装div的最后两个div并隐藏它们)
If the script tag is always before the div's that need removing you could do this - 如果脚本标记始终位于需要删除的div之前,则可以执行此操作 -
$('.right-panel > script').nextAll('div').remove();
http://jsfiddle.net/w6d8K/1/ http://jsfiddle.net/w6d8K/1/
Based on what you tried you could do this - 根据你的尝试你可以做到这一点 -
$('#the-form').nextAll('div').hide();
http://jsfiddle.net/w6d8K/2/ http://jsfiddle.net/w6d8K/2/
Here are the docs for nextAll() - https://api.jquery.com/nextAll/ 以下是nextAll()的文档 - https://api.jquery.com/nextAll/
The simplest route would be to add classes to the dynamic elements. 最简单的方法是向动态元素添加类。 Something like:
就像是:
<div class="removable-element" id="dynamic-id-1">Advertisement 1</div>
Then, you can do something like: 然后,您可以执行以下操作:
$(".right-panel .removable-element").remove()
If only one div at a time is generated dynamically. 如果动态生成一次只有一个div。 Add this to dynamic generation:
将其添加到动态生成:
$('#the-form + div').hide();
Another method to achieve the same (not preferred) is: 实现相同(不是优选)的另一种方法是:
$('#the-form').next('div').remove();
You are saying you don't want to target their "id", but is there some specific part in the id that will remain the same ? 你是说你不想以他们的“id”为目标,但是在id中是否会有一些特定的部分保持不变?
like for instance "dynamic-id-" ? 比如“dynamic-id-”?
If this is the case you can target them by using a "like selector". 如果是这种情况,您可以使用“like selector”来定位它们。 The code below would target all divs whose ID is starting with "dynamic-id"
下面的代码将针对ID以“dynamic-id”开头的所有div
$('div[id^=dynamic-id]').each(function () {
//do something here
});
Target the class instead of the dynamic ID. 定位类而不是动态ID。
<div class="element-to-remove" id="dynamic-id-1" />
<div class="element-to-remove" id="dynamic-id-2" />
$('.right-panel . element-to-remove').remove();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.