简体   繁体   English

如何仅通过前n个字母获取ID

[英]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. 如果您期望selectortest_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.

相关问题 如何只获取数组的前 n%? - How to get only the first n% of an array? AmCharts 地图 - 如何只获得国家名称的 2 个首字母? - AmCharts maps - how to get only 2 first letters of country name? 尝试获取提示以将提示的前三个字符限制为仅字母 - Trying to get a prompt to limit the first three character of a prompt to letters only 如何获取父div的ID,然后取出名称的前3个字母并将名称的最后一位存储到变量中? - How do I get the id of parent div, then take out the first 3 letters of the name and store the last digit of the name into a variable? 如何验证输入文本允许第一和第二位数字仅为字母 - How to verify that the input text allows the first and second digits to be only letters 如何使用jQuery过滤器功能仅捕获前几个字母? - How to capture only the first few letters using jQuery filter function? 如何在 JQuery 或 Java 中获取字符串的前三个字母? - How get the first three letters of a string in JQuery or Java? 如何获得JavaScript中每个单词的首字母? - How can I get the first letters of each word in JavaScript? 如果只有3个以上的字母,如何获取变量 - how to get varaiable if only have more than 3 letters javascript 如何在输入字段中至少包含n个字母的情况下将Google地方信息自动填充功能配置为仅执行请求? - How to configure Google Places Autocomplete only to do requests if there are at least n letters in the input field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM