【发布时间】:2017-02-05 18:08:28
【问题描述】:
我正在做以下事情:
return this.innerHTML = `
<input
value = ${this.value ? this.value : ''}
placeholder=${this.placeholder}
></input>
`
现在的问题是,如果 this.value 未定义,未定义的字符串设置为输入值,这就是为什么我尝试将空字符串返回到输入值属性。这不起作用,然后我将placeholder=${this.placeholder} 作为字符串放入 value 属性中。
我在谷歌上安静了一会儿,找不到任何答案,这可能吗?
我们将不胜感激。
【问题讨论】:
-
您可以使用
this.value || ''代替this.value ? this.value : ''以简化和可读性。
标签: javascript ecmascript-6 template-strings