[英]Using conditionals in javascript template literal
I store a value in my localstorage and based on that value I would like to decide the styling inside a template literal notation. 我将一个值存储在我的本地存储中,并根据该值决定模板文字符号内的样式。
here I have some variables: 这里我有一些变量:
var right = localStorage.getItem("shareBarPosition") === "right";
var posRight = `#socialShare {
left: auto;
right: 0 !important;
}`;
var posLeft = `#socialShare {
left: 0 !important;
right: auto;
}`;
which I would like to use in a template literal notation like this: 我想在这样的模板文字符号中使用:
document.querySelector("#shareButton-code").innerHTML += `<style>
/* Button specific styling */
${right ? posRight : posLeft}
</style>`
But this will always show the posRight styling. 但这将始终显示posRight样式。 Why is that?
这是为什么?
I had to change the innerHTML +=
to innerHTML =
我不得不将
innerHTML +=
更改为innerHTML =
document.querySelector("#shareButton-code").innerHTML = `<style>
/* Button specific styling */
${!right ? posLeft : posRight}
</style>`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.