Javascript 在querySelectorAll上按id排除特定元素

Javascript 在querySelectorAll上按id排除特定元素,javascript,css-selectors,Javascript,Css Selectors,我尝试从以下位置获取数组的长度: document.querySelectorAll('select,input,textarea'); alert(Object.keys(arr).length);//19 在该数组中,我必须排除4个元素,即2个input type=“hidden”,以及2个带有specifiid的,因此我尝试使用:而不是选择器: document.querySelectorAll('select,input,textarea,input:not[type="hidden"

我尝试从以下位置获取数组的长度:

document.querySelectorAll('select,input,textarea');
alert(Object.keys(arr).length);//19
在该数组中,我必须排除4个元素,即2个
input type=“hidden”
,以及2个带有specifi
id的
,因此我尝试使用
:而不是选择器

document.querySelectorAll('select,input,textarea,input:not[type="hidden",input:not[id="input_up_img_perfil"],input:not[id="sub_img_perfil"],');
alert(Object.keys(arr).length);//19

对于该查询,什么是正确的sintax?

您可以将节点集合转换为数组,然后使用
array#filter
筛选出不需要的元素:

Array.from(document.querySelectorAll('select,input,textarea'))
  .filter(item => item.type !== 'hidden' || item.id !== 'input_up_img_perfil' || item.id !== 'sub_img_perfil');

您可以将节点集合转换为数组,然后使用
array#filter
过滤掉非必需的元素:

Array.from(document.querySelectorAll('select,input,textarea'))
  .filter(item => item.type !== 'hidden' || item.id !== 'input_up_img_perfil' || item.id !== 'sub_img_perfil');
比如说

<input class="test">
<input class="test asd">
而不是:

document.querySelectorAll('select,input,textarea,input:not[type="hidden",input:not[id="input_up_img_perfil"],input:not[id="sub_img_perfil"],');
比如说

<input class="test">
<input class="test asd">
而不是:

document.querySelectorAll('select,input,textarea,input:not[type="hidden",input:not[id="input_up_img_perfil"],input:not[id="sub_img_perfil"],');
试试这个:

document.querySelectorAll('select,input:not([type="hidden"]):not(#input_up_img_perfil):not(#sub_img_perfil),textarea');
它应该工作得很好;)

它实际上非常简单:首先,需要向
:not
操作符添加括号。然后,您需要考虑正确的CSS查询来选择您需要的内容

不起作用的示例:

'input:not([type="hidden"]),input:not(#input_up_img_perfil),input:not(#sub_img_perfil)'
因为您实际上有三个查询,结果将在最后合并,因为
input:not(#input\u up\u img\u perfil)
对隐藏字段没有约束,所以即使您设置了
input:not([type=“hidden”])
,也会在结果中得到它们

这就是为什么您需要执行以下操作:

'input:not([type="hidden"]):not(#input_up_img_perfil):not(#sub_img_perfil)'
在这里,输入标记上只有一个查询,有三个约束

希望这是清楚的;)

试试这个:

document.querySelectorAll('select,input:not([type="hidden"]):not(#input_up_img_perfil):not(#sub_img_perfil),textarea');
它应该工作得很好;)

它实际上非常简单:首先,需要向
:not
操作符添加括号。然后,您需要考虑正确的CSS查询来选择您需要的内容

不起作用的示例:

'input:not([type="hidden"]),input:not(#input_up_img_perfil),input:not(#sub_img_perfil)'
因为您实际上有三个查询,结果将在最后合并,因为
input:not(#input\u up\u img\u perfil)
对隐藏字段没有约束,所以即使您设置了
input:not([type=“hidden”])
,也会在结果中得到它们

这就是为什么您需要执行以下操作:

'input:not([type="hidden"]):not(#input_up_img_perfil):not(#sub_img_perfil)'
在这里,输入标记上只有一个查询,有三个约束


希望这是清楚的;)

tks代表atention,我需要使用
排除元素和:not selector
way,你知道怎么做吗?tks代表atention,我需要使用
排除元素和:not selector
way,你知道怎么做吗?
输入:not('[type=hidden]')
,你缺少否定(the)操作符的括号,以及应该出现在第一个
“隐藏”
之后的结束
]
字符。如果可能的话,请尝试任何技巧,使问题变得更好<代码>输入:不是('[type=hidden]'),您缺少了否定(the)运算符的括号,以及应该出现在第一个
“hidden”
后面的结束
]
字符。如果可能的话,请尝试任何技巧,使问题变得更好!表演,tkssss!谢谢(原始注释已删除,因为这将很快被删除。)执行,tkssss!谢谢(原始注释已删除,因为这将很快删除。)