[英]successfully reverse the transition with js ,but why my another way doesn't work?
let dbclickre = true;
function flipped() {
if (dbclickre) {
document.querySelector(".linkrec").setAttribute("Id", "flipped");
} else {
document.querySelector(".linkrec").removeAttribute("Id", "flipped")
}
dbclickre = !dbclickre;
}
// const dbclickre = document.querySelector(".reverse");
// function flipped() {
// if (dbclickre.style.backgroundColor = 'white') {
// document.querySelector(".linkrec").setAttribute("Id", "flipped");
// } else {
// document.querySelector(".linkrec").removeAttribute("Id", "flipped")
// }
// }
Instead of relying on background color for checking the state of flip, you could check for existence of Id
attribute.您可以检查是否存在
Id
属性,而不是依赖背景颜色来检查翻转的 state。 Here the the changed code:这里是更改后的代码:
const dbclickre = document.querySelector(".reverse");
function flipped() {
if ( document.querySelector(".linkrec").getAttribute("Id") == undefined ) {
document.querySelector(".linkrec").setAttribute("Id", "flipped");
} else {
document.querySelector(".linkrec").removeAttribute("Id", "flipped")
}
}
Why element.style
would not work?为什么
element.style
不起作用?
From MDN Web Docs :来自MDN Web 文档:
The
style
property is used to get as well as set the inline style of an element.style
属性用于获取和设置元素的内联样式。
Hence, the style
property would not work with embedded or external CSS.因此,
style
属性不适用于嵌入式或外部 CSS。
Also, it may not be a good idea to use hard-coded colors as the condition, because changing colors in the respective CSS classes would completely break the functionality.此外,使用硬编码的 colors 作为条件可能不是一个好主意,因为在相应的 CSS 类中更改 colors 会完全破坏功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.