Javascript 如何在js中选择id中包含特定文本的select type对象?
我有html元素,它们的ID中有关键字“target”。我想选择一个“选择”元素,该元素的id中包含关键字Javascript 如何在js中选择id中包含特定文本的select type对象?,javascript,jquery,html,Javascript,Jquery,Html,我有html元素,它们的ID中有关键字“target”。我想选择一个“选择”元素,该元素的id中包含关键字 <select id="page_blocks_0_buttons_1_target"><option value=""></option></select> <select id="page_blocks_0_buttons_2_target"><option value=""></option><
<select id="page_blocks_0_buttons_1_target"><option value=""></option></select>
<select id="page_blocks_0_buttons_2_target"><option value=""></option></select>
<select id="page_blocks_0_buttons_3_target"><option value=""></option></select>
<select id="page_blocks_0_buttons_4_target"><option value=""></option></select>
<div id="page_target_div"></div>
使用
:not
避免div
var targetFields=document.queryselectoral(“*[id*=target]:not(div)”);
console.log(targetFields.length)代码>
需要一个CSS选择器——您在前面加上*
,它将选择所有元素——将其更改为select
,它应该可以正常工作。请参见下面的代码段:
var targetFields=document.queryselectoral(“选择[id*=target]”);
console.log(targetFields)
这应该可以var targetFields=document.queryselectoral(“选择[id*=target]”)代码>
var targetFields = document.querySelectorAll("*[id*=target]");