简体   繁体   English

JavaScript:使用通配符从PARENT元素中选择所有子元素

[英]JavaScript: Use a wildcard to select all child elements from a PARENT element

I need to select ALL child elements from <div id="VidContainer"> who's ID's pattern is abc- and then a randomly generated number, followed by -container . 我需要选择从所有子元素 <div id="VidContainer">谁的ID的图案是abc-然后随机生成的数字,接着-container Ej, "abc-985224562456540-container" Ej, "abc-985224562456540-container"

Because the numbers in the middle are randomly generated, a loop won't work. 由于中间的数字是随机生成的,因此循环将不起作用。 When the numbers have more than 10 digits, JS's engine enters into an "eternal loop" and the browser crashes. 当数字超过10位时,JS的引擎进入“永恒循环”,浏览器崩溃。

After selecting the elemnets , I need to delete all <div found, with the matching pattern. 选择了elemnets之后 ,我需要删除所有具有匹配模式的<div

What would be the wildcard JavaScript needs to use to select them? JavaScript需要使用哪些通配符来选择它们?
And what would be the best approach to delete those elements? 删除这些元素的最佳方法是什么?

Dummy HTML : 虚拟HTML

<div id="VidContainer">
  ...some more html...
  <div id="abc-13-container">Text 13</div>
  ...some more html...
  <div id="abc-9999-container">Text 10</div>
  ...some more html...
  <div id="abc-21540540640-container">Text 19</div>
  ...some more html...
</div>

I got most of the code. 我掌握了大部分代码。 I just can't find a way to get the child element's ID ( wildcard ) 我只是找不到一种获取子元素的ID( wildcard )的方法

//Defind element ID
var elementID = "abc" + wildcard + "-container";
var parent = document.getElementById("VidContainer");

//Selet all elements to be deleted
var elements = document.getElementById(elementID);

// Removes an element from the document
while (elements.length) { parent.parentNode.removeChild(elements[0]); }

You can match both the start and end of an attribute with a CSS selector: 您可以使用CSS选择器来匹配属性的开始和结束:

 var items = document.querySelectorAll("#VidContainer>[id^=abc-][id$=-container]"); console.log("matching items to delete: ", items.length); for (let item of items) item.remove(); 
 <div id="VidContainer"> <div id="abc-13-container">Text 13</div> <div id="abc-9999-container">Text 10</div> <div id="hithere">Hi There</div> <div id="abc-21540540640-container">Text 19</div> <div id="something">Something</div> </div> 

You could use querySelectorAll() with the appropriate CSS selector. 您可以将querySelectorAll()与适当的CSS选择器一起使用。 Not a wildcard solution, but still: 不是通配符解决方案,但仍然:

 // arrow function to get all the elements // that are (still) part of the DOM const getElements = () => { return document.querySelectorAll('#VidContainer [id^="abc-"][id$="-container"]') } // arrow function to DRY code removal const remE = e => e.parentNode.removeChild(e) // adding "self-delete on click" to all appropriate elements getElements().forEach(e => { e.addEventListener('click', function(e) { remE(e.target) }) }) // adding "delete all appropriate elements on click" document.getElementById('clearAll').addEventListener('click', function(e) { getElements().forEach(e => { remE(e) }) }) 
 [id^='abc-'][id$='-container'] { font-weight: 700; cursor: pointer; } 
 <div id="VidContainer"> ...some more html... <div id="abc-13-container">Text 13 - click text to remove item</div> ...some more html... <div id="abc-9999-container">Text 10 - click text to remove item</div> ...some more html... <div id="abc-21540540640-container">Text 19 - click text to remove item</div> ...some more html... </div> <button id="clearAll">Clear all</button> 

This is a solution that has all the functions for correct removal (one-by-one or all at once). 该解决方案具有正确删除(一次或一次全部删除)的所有功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM