Javascript 单击功能仅在第二次单击时工作

Javascript 单击功能仅在第二次单击时工作,javascript,php,wordpress,Javascript,Php,Wordpress,在Wordpress/woomercebuild上具有以下代码。问题是该按钮仅在第二次单击时起作用。第一次点击就需要这个,我的代码怎么了 // Add button to hide Personalisation add_action('woocommerce_single_product_summary', 'custom_text', 29); function custom_text() { print '<button class="personlisation-but

Wordpress/woomerce
build上具有以下代码。问题是该按钮仅在第二次单击时起作用。第一次点击就需要这个,我的代码怎么了

// 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>';  
}  

//添加按钮以隐藏个性化设置
添加行动(“商业单品摘要”、“自定义文本”,29);
函数自定义_text(){
打印“添加个性化设置”;
}  
//切换可见性的脚本
函数myFunction(){
var x=document.getElementById(“wcj_产品_插件”);
如果(x.style.display==“无”){
x、 style.display=“block”;
}否则{
x、 style.display=“无”;
}
}

最好添加一个检查
null
。页面上可能没有这样的元素

函数myFunction(){
var x=document.getElementById(“演示”);
如果(x!=null&&x.style.display==“无”){
x、 style.display=“block”;
}否则{
x、 style.display=“无”;
}
}

切换

Hello world
Hello,您应该注释myFunction代码并仅设置警报(“Hello”);如果在单击时调用了on-click alert,则表示您的函数调用完美,问题将出现在document.getElementById(“wcj_产品_插件”)中;您可能传递了错误的id。请检查它,让我知道它是否有效,谢谢。