参数与 ' 之间有什么区别? '和参数不带 ' '在模板文字 (js) 中
JavaScript 中的模板文字中带引号的参数和不带引号的参数有什么区别
为什么当我把引号放在>>时? ('${key}') 它的功能是>> fun('${key}') 而当我删除引用时 >> (${key}) 它不执行函数>>乐趣(${键}) 使用 JavaScript 的模板文字中带引号的参数和不带引号的参数有什么区别
for ( let key in states) {
console.log(key);
var n =states[key].name;
var cur = states[key].currency;
console.log()
con.innerHTML += `<div> <span>name : " ${n}
" </span> <br> <span>cur : " ${cur}
" </span> </div><br> <span>cur : " ${key}
" </span> <button onclick= fun('${key}')> select </button><hr> `
}
function fun(o) {
alert(states[o].name);
alert(states[o].currency);
}
上面的代码运行函数>>有趣('${key}')
for (let key in states) {
console.log(key);
var n = states[key].name;
var cur = states[key].currency;
console.log()
con.innerHTML += `<div> <span>name : " ${n}
" </span> <br> <span>cur : " ${cur}
" </span> </div><br> <span>cur : " ${key}
" </span> <button onclick= fun(${key})> select </button><hr> `
}
function fun(o) {
alert(states[o].name);
alert(states[o].currency);
}
What is the difference between parameter with quotation and parameter without quotation in Template literals in JavaScript
Why when I put quotation >> ('${key}') it does the function >> fun('${key}') while when I delete quotation >> (${key}) it doesn't do the function >> fun(${key})
what different between parameter with quotation and parameter without quotation in Template literals with JavaScript
for ( let key in states) {
console.log(key);
var n =states[key].name;
var cur = states[key].currency;
console.log()
con.innerHTML += `<div> <span>name : " ${n}
" </span> <br> <span>cur : " ${cur}
" </span> </div><br> <span>cur : " ${key}
" </span> <button onclick= fun('${key}')> select </button><hr> `
}
function fun(o) {
alert(states[o].name);
alert(states[o].currency);
}
the code above run the function >> fun('${key}')
for (let key in states) {
console.log(key);
var n = states[key].name;
var cur = states[key].currency;
console.log()
con.innerHTML += `<div> <span>name : " ${n}
" </span> <br> <span>cur : " ${cur}
" </span> </div><br> <span>cur : " ${key}
" </span> <button onclick= fun(${key})> select </button><hr> `
}
function fun(o) {
alert(states[o].name);
alert(states[o].currency);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它只是在最终字符串中添加
'
。这在您的示例中有效,但当您省略它们时却不起作用,因为您正在使用它在
HTML
处理程序内生成js
代码。您的键是字符串,因此在
js
中,它们需要括在'
或"
中。当您省略它们时,它们将被视为变量,并且很可能您没有具有该名称的变量(或常量)。It just adds
'
in the final string.The reason why this works in your example, but when you omit them it doesn't is because you are using that to generate
js
code inside anHTML
handler.Your keys are strings, so in
js
they need to be enclosed in either'
or"
. When you omit them they are treated as variables and most likely you don't have variables (or constants) with that name(s).