[英]How to get ID by first n letters only
enter image description here 在此处输入图片说明
<g id="test_model/sub1/line_uniq_id_2"> </g> <g id="test_model/sub1/line2_uniq_id_3"> </g>
i want to extract ID by first n letters in this case after test_model/sub1/line_uniq_id_ there can be any number so how i can write that check only test_model/sub1/line_uniq_id because instead of _2 there can be any number. 我想在这种情况下通过test_model / sub1 / line_uniq_id_之后的前n个字母提取ID,所以可以有任何数字,所以我该怎么写才能只检查test_model / sub1 / line_uniq_id,因为不是_2可以有任何数字。
means if i give test_model/sub1/line_uniq_id_ it will take it as test_model/sub1/line_uniq_id_anything (anything can be any number)in this case it is 2 意味着如果我给test_model / sub1 / line_uniq_id_,它将作为test_model / sub1 / line_uniq_id_anything(任何东西都可以是任何数字)在这种情况下为2
if i give test_model/sub1/line2_uniq_id_ it will take it as test_model/sub1/line_uniq_id_anything (anything can be any number)in this case it is 3 如果我给test_model / sub1 / line2_uniq_id_,它将作为test_model / sub1 / line_uniq_id_anything(任何东西都可以是任何数字)在这种情况下为3
You need JavaScript. 您需要JavaScript。
You can get a list of all IDs starting with test_model/sub1/line_uniq
using document.querySelectorAll("[id^='test_model/sub1/line_uniq']")
您可以使用document.querySelectorAll("[id^='test_model/sub1/line_uniq']")
获取以test_model/sub1/line_uniq
开头的所有ID的列表。
but that will only find ONE of your <g>
s since they have different /lineX/ 但这只会找到您的<g>
因为它们的/ lineX /
This one will find both 这个会找到两个
function getIds() { let tags = document.querySelectorAll("g"); let ids=[]; for (let tag of tags) { ids.push(tag.id.split("_").slice(0,-1).join("_")); // use (-1) to just get the number } return ids; } console.log(getIds());
<g id="test_model/sub1/line_uniq_id_2"> </g> <g id="test_model/sub1/line2_uniq_id_3"> </g>
If you are expecting a selector
for which starts with test_model/sub1/line_uniq_id_
then here is the solution. 如果您期望selector
以test_model/sub1/line_uniq_id_
那么这里就是解决方案。
javascript selector javascript选择器
var abc = document.querySelectorAll('[id^="test_model/sub1/line_uniq_id_"]');
css selector CSS选择器
[id^="test_model/sub1/line_uniq_id_"]{
//some css here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.