[英]Html Jquery Help Whats Wrong
I want a Boolean for a button stat but i cant what's wrong in my code?我想要一个 Boolean 作为按钮统计信息,但我不知道我的代码有什么问题? `
`
<button id="read-more" class="read-more" onclick="Openreadmore()"> Read More </button>
`` ``
` `
<script>
var status = false;
var myopacity = 0;
function Openreadmore() {
if (status == false) {
document.getElementById("text-more").style.display = "none";
document.getElementById("text-more").classList.remove("is-active");
document.getElementById("small-text").style.display = "block";
document.getElementById("read-more").textContent = 'Read More';
status == true;
}
if (status == true) {
document.getElementById("text-more").style.display = "block";
MycomeFadeFunction();
document.getElementById("text-more").classList.toggle("is-active");
document.getElementById("small-text").style.display = "none";
status == false;
document.getElementById("read-more").textContent = 'Close Paragraph';
}
}
function MycomeFadeFunction() {
if (myopacity < 1) {
myopacity += .075;
setTimeout(function() {
MycomeFadeFunction()
}, 100);
}
document.getElementById('text-more').style.opacity = myopacity;
}
</script>
` I wanted to hide some divs when the boolean is true or false, there is a var status = false; ` 我想在 boolean 为 true 或 false 时隐藏一些 div,有一个 var status = false; I set it as default value then checked with if but things didn't work out
我将其设置为默认值,然后检查 if 但没有成功
change var type to let type and check更改 var 类型以让类型和检查
//var status = false;
//var myopacity = 0;
let status = false;
let myopacity = 0;
The issue seems to be that your var status
takes a string value instead of a boolean.问题似乎是您的
var status
采用字符串值而不是 boolean。
var status = false; function Openreadmore() { if (status == "false") { status="true"; document.getElementById("textDiv").style.color= "red"; /*Your code*/ } else if (status == "true") { status = "false"; document.getElementById("textDiv").style.color= "blue"; /*Your code*/ } }
<button id="btnReadMore" class="read-more" onclick="Openreadmore()"> Read More </button> <div id="textDiv"> button changes color of this. </div>
Something like this in JS code should give your desired result. JS 代码中的类似内容应该会给出您想要的结果。
You are using the wrong number of ='s when setting the value of status inside the if statement, as i understand you are trying to create some sort of "toggle"在 if 语句中设置状态值时,您使用了错误数量的 =,据我所知,您正在尝试创建某种“切换”
if (status == true) {
document.getElementById("text-more").style.display = "block";
MycomeFadeFunction();
document.getElementById("text-more").classList.toggle("is-active");
document.getElementById("small-text").style.display = "none";
//this was status == false previously, which would return a boolean rather than set the value
status = false;
document.getElementById("read-more").textContent = 'Close Paragraph';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.