简体   繁体   English

为什么我收到querySelectorAll()错误

[英]why i'm getting a querySelectorAll() error

i tried to implement a document.getElementsByAttribute function and i came with this : 我试图实现document.getElementsByAttribute函数,但我附带了以下内容:

function getElementsByAttribute(att, val){

    return document.querySelectorAll(`[${att} = ${val}]`)

}

but why i get an error when i try to type the return statement directly on the console : 但是为什么当我尝试直接在控制台上键入return语句时出现错误:

document.querySelectorAll("["id" = "btn"]")

is it not the same like getElementsByAttribute2('id', 'btn') ? getElementsByAttribute2('id', 'btn')吗?

but why i get an error when i try to type the return statement directly on the console : 但是为什么当我尝试直接在控制台上键入return语句时出现错误:

document.querySelectorAll("["id" = "btn"]")

Because what you are typing into the console is not the same as what you have in your function. 因为您在控制台中输入的内容与您在功能中所拥有的不同。 Your function uses template literals : 您的函数使用模板文字

document.querySelectorAll(`[${att} = ${val}]`)

And, template literals can be inserted directly into regular literal strings without having to worry about quoting them or concatenating them with the rest of the string. 而且,模板文字可以直接插入到常规文字字符串中,而不必担心引用它们或将它们与字符串的其余部分连接在一起。

When you enter directly into the console, you didn't use template literals, you just used a literal and in that case quotes and concatenation matter. 直接输入控制台时,您没有使用模板文字,而只是使用文字,在这种情况下,引号和连接很重要。 Quotes come in pairs, so the first pair is: "[" and that is immediately followed by id and because you didn't concatenate the two parts like this: 引号是成对出现的,因此第一对是: "[" ,其后紧跟id ,因为您没有将这两个部分串联在一起:

"[" + id

It throws an error - probably something like unexpected identifier . 它将引发错误-可能类似于unexpected identifier

The CSS attribute selector should take the form of: CSS属性选择器应采用以下形式:

[attributeName comparisonOperator value]

Quotes around the value are only necessary if the value has spaces in it. 仅当值中包含空格时,才需要在值周围加上引号。 So you could just write: 所以你可以这样写:

document.querySelectorAll("[id=btn]");

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

相关问题 为什么我在 querySelectorAll 上收到错误 TS2339 for checkbox property &#39;checked&#39; 不存在 - Why am I getting error TS2339 on querySelectorAll for checkbox property 'checked' does not exist 出现错误:req.validatonErrors 不是 function 但我不知道为什么 - Getting error: req.validatonErrors is not a function but I'm not sure why 为什么我会收到错误消息:“ SyntaxError:意外令牌&lt;”? - Why i'm getting error: “SyntaxError: Unexpected token <”? 为什么我在反应原生 windows 时遇到 os 模块错误 - Why I'm getting os module error in react native windows 有人可以解释为什么我收到 500 内部错误吗? - Can someone explain why I'm getting 500 Internal Error? 为什么我在安装 axios 时出错 - Why i'm getting error while install axios 我收到了NaN错误 - I'm getting a NaN error 为什么我收到错误“错误:语法错误,无法识别的表达式:不支持的伪:选择”? - Why I'm getting the error “Error: Syntax error, unrecognized expression: unsupported pseudo: select”? 当我使用javascript更改标签文本时,为什么querySelectorAll不是函数错误弹出? - why querySelectorAll is not a function error pop up when i change the text of a tag using javascript? 为什么当我尝试运行服务器 gRPC 时出现此错误? - why i'm i getting this error when i try to run my server gRPC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM