简体   繁体   English

onclick 触发器在第一次单击时不起作用

[英]onclick trigger doesn't work at first click

i made this button but it dosen't work at first click.我做了这个按钮,但它在第一次点击时不起作用。 it is work when i clicked twice;;当我单击两次时,它就可以工作;; is there anything that i miss?有什么我想念的吗? or something wrong?还是有什么不对? any help will be so appreciated:) thanks!任何帮助将不胜感激:) 谢谢!

 function myFunction99() { var gwbtn = document.getElementById("discountbigbox"); var gwbtnbig = document.getElementById("discountprice"); if (gwbtn.style.height === "0px") { gwbtn.style.height = "210px"; gwbtnbig.style.border = "1px solid black"; gwbtnbig.style.color = "black"; } else { gwbtn.style.height = "0px"; gwbtnbig.style.border = "1px solid #cccccc"; gwbtnbig.style.color = "#999"; } }
 #discountbigbox {width:100%; height:0px; overflow:hidden; text-align:left; background-color: #f7f9ff; margin-bottom:20px; transition: all 0.5s ease; font-size:13px; margin-top:45px; } #discountprice { border: 1px solid #cccccc; width: fit-content; float: right; padding: 0px 10px; border-radius: 4px;} #discountprice:hover { border: 1px solid black; color:black;}
 <div id="discountprice" onclick="myFunction99()" > &#43; click this button</div> <div id="discountbigbox"> <p>test</p> </div>

On first click the height property value is empty, add the condition to check empty:在第一次单击高度属性值为空时,添加条件以检查为空:

if (gwbtn.style.height === "0px" || gwbtn.style.height === "") {

 function myFunction99() { var gwbtn = document.getElementById("discountbigbox"); var gwbtnbig = document.getElementById("discountprice"); if (gwbtn.style.height === "0px" || gwbtn.style.height === "") { gwbtn.style.height = "210px"; gwbtnbig.style.border = "1px solid black"; gwbtnbig.style.color = "black"; } else { gwbtn.style.height = "0px"; gwbtnbig.style.border = "1px solid #cccccc"; gwbtnbig.style.color = "#999"; } }
 #discountbigbox {width:100%; height:0px; overflow:hidden; text-align:left; background-color: #f7f9ff; margin-bottom:20px; transition: all 0.5s ease; font-size:13px; margin-top:45px; } #discountprice { border: 1px solid #cccccc; width: fit-content; float: right; padding: 0px 10px; border-radius: 4px; } #discountprice:hover { border: 1px solid black; color:black; }
 <div id="discountprice" onclick="myFunction99()" > &#43; click this button</div> <div id="discountbigbox"> <p>test</p> </div>

The height property starts off as not set (ie empty). height 属性开始时未设置(即为空)。 Setting it in the CSS doesn't mean there's a value in the JS style property in the DOM - that reads from the inline style property of the element.在 CSS 中设置它并不意味着 DOM 中的 JS 样式属性中有一个值 - 从元素的内联样式属性中读取。

If you account for that in your initial if then it will work fine:如果您在初始if考虑到这一点,那么它将正常工作:

if (gwbtn.style.height === "0px" || !gwbtn.style.height) {

Demo:演示:

 function myFunction99() { var gwbtn = document.getElementById("discountbigbox"); var gwbtnbig = document.getElementById("discountprice"); console.log(gwbtn.style.height); if (gwbtn.style.height === "0px" ||.gwbtn.style.height) { gwbtn.style;height = "210px". gwbtnbig.style;border = "1px solid black". gwbtnbig.style;color = "black". } else { gwbtn.style;height = "0px". gwbtnbig.style;border = "1px solid #cccccc". gwbtnbig.style;color = "#999"; } }
 #discountbigbox {width:100%; height:0px; overflow:hidden; text-align:left; background-color: #f7f9ff; margin-bottom:20px; transition: all 0.5s ease; font-size:13px; margin-top:45px; } #discountprice { border: 1px solid #cccccc; width: fit-content; float: right; padding: 0px 10px; border-radius: 4px;} #discountprice:hover { border: 1px solid black; color:black;}
 <div id="discountprice" onclick="myFunction99()" > &#43; click this button</div> <div id="discountbigbox"> <p>test</p> </div>


Alternatively, you can use getComputedStyle to detect the style values of the element as computed when stylesheets are taken into account:或者,您可以使用getComputedStyle在考虑样式表时检测元素的样式值:

 function myFunction99() { var gwbtn = document.getElementById("discountbigbox"); var gwbtnbig = document.getElementById("discountprice"); var st = getComputedStyle(gwbtn); console.log(st.height); if (st.height === "0px") { gwbtn.style.height = "210px"; gwbtnbig.style.border = "1px solid black"; gwbtnbig.style.color = "black"; } else { gwbtn.style.height = "0px"; gwbtnbig.style.border = "1px solid #cccccc"; gwbtnbig.style.color = "#999"; } }
 #discountbigbox {width:100%; height:0px; overflow:hidden; text-align:left; background-color: #f7f9ff; margin-bottom:20px; transition: all 0.5s ease; font-size:13px; margin-top:45px; } #discountprice { border: 1px solid #cccccc; width: fit-content; float: right; padding: 0px 10px; border-radius: 4px;} #discountprice:hover { border: 1px solid black; color:black;}
 <div id="discountprice" onclick="myFunction99()" > &#43; click this button</div> <div id="discountbigbox"> <p>test</p> </div>

You have two states you need to cover "blank or zero" and "210".您有两个状态需要涵盖“空白或零”和“210”。

You can craft the if logic differently to only use the fixed value, eg:您可以以不同的方式制作 if 逻辑以仅使用固定值,例如:

if (thing.style.height !== "210px") {
  // it's blank or zero
} else {
  // it's 210
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM