Wordpress/Woocommerce在構建中有以下代碼。問題是該按鈕僅在第二次單擊時有效。需要這個在第一次點擊時工作,我的代碼有什么問題?// Add button to hide Personalisationadd_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>
1 回答

料青山看我應如是
TA貢獻1772條經驗 獲得超8個贊
最好添加一個檢查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>
- 1 回答
- 0 關注
- 111 瀏覽
添加回答
舉報
0/150
提交
取消