[英]On Click function only working on second click
在Wordpress/Woocommerce
構建中使用以下代碼。 問題是該按鈕僅適用於第二次點擊。 需要在第一次點擊時使用它,我的代碼有什么問題?
// Add button to hide Personalisation
add_action('woocommerce_single_product_summary', 'custom_text', 29);
function custom_text() {
print '<button class="personlisation-button" onclick="myFunction()">Add Personalisation</button>';
}
// Script to toggle visibility
<script type="text/javascript">
function myFunction() {
var x = document.getElementById("wcj_product_addons");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
添加對null
的檢查會很好。 您可能在頁面上沒有這樣的元素。
function myFunction() { var x = document.getElementById("demo"); if (x != null && x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } }
<button onclick="myFunction()" > Toggle</button > <div id="demo">Hello world</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.