简体   繁体   English

.toogle(),但在2或3上单击

[英].toogle() but on 2 or 3 click

Wordpress personnal project, I'm a bit disapointed : WordPress的个人项目,我有点失望:

I have created a button and a div from shortcode (function.php): 我已经从简码(function.php)创建了一个按钮和一个div:

(into my shortcode function) (进入我的简码功能)

echo '<br /><br /><br /><button id="button_detail_sc2" onclick="showhide_detail_sc2()">Take a look on the div</button><br />';
echo '<div id="resume_quest_sc_2">XXXXXXXX</div>';

I made a function for hide/display the div 我做了一个隐藏/显示div的功能

function showhide_detail_sc2()
{
jQuery("#button_detail_sc2").click(function() {jQuery("#resume_quest_sc_2").toggle();  });}

Everything works fine BUT I have to click 3 time on the button to hide the div the first time, and after have to click 2 time to display or hide again the div. 一切正常,但是我必须单击3次按钮才能第一次隐藏div,然后必须单击2次才能显示或再次隐藏div。

I don't understand, I just want to hide/display on 1 click everytime. 我不明白,我只想每次单击一次即可隐藏/显示。

Could you help me ? 你可以帮帮我吗 ?

The function jQuery("#button_detail_sc2").click(...) adds an event handler for the click event on your button. 函数jQuery("#button_detail_sc2").click(...)为按钮上的click事件添加事件处理程序。 That is not necessary since you are using the onclick attribute, so the function showhide_detail_sc2() gets executed every time the button is clicked. 由于您使用的是onclick属性,因此这不是必需的,因此每次单击按钮时都会执行函数showhide_detail_sc2()

Try executing the toggle command directly: 尝试直接执行toggle命令:

function showhide_detail_sc2()
{
    jQuery("#resume_quest_sc_2").toggle();
}

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

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