简体   繁体   English

使用JavaScript进行事件调用

[英]Event call using javascript

I want to invoke Javascript function with call on <a> to hide and display block using divid as id like in the code given below. 我想调用JavaScript函数调用上<a>使用隐藏和显示模块divid作为id喜欢在下面给出的代码。 I am able to hide the open block but not open it. 我可以隐藏打开的块,但不能打开它。

 echo "<span class='bold' style='background:#0DCAD1'><a name='form_a_$group_seq' href='#div_$group_seq' style='float:left;color:white' id='form_a_$group_seq' value='1' " .
"onclick='return divclick(this,\"div_$group_seq\");'";
 if ($display_style == 'block') echo "clicked";

 // Modified 6-09 by BM - Translate if applicable  
 echo "<b>" . xl_layout_label($group_name) . "</b></a></span>\n";

 echo "<div id='div_$group_seq' class='section' style='display:$display_style;'>\n";
 echo " <table border='0' cellpadding='0'>\n";
 $display_style = 'none';
}
else if (strlen($last_group) == 0) {
echo " <table border='0' cellpadding='0'>\n";
}

This is the javascript code i am using. 这是我正在使用的javascript代码。

function divclick(a, divid) {
var divstyle = document.getElementById(divid).style;
if(a.clicked) {
divstyle.display = 'block';
} else {
divstyle.display = 'none';
}
return true;
}

You need to check what the style is current set to and act accordingly. 您需要检查当前设置的样式并采取相应的措施。 If you can provide a jsfiddle, I can probably demo it for you. 如果您可以提供jsfiddle,我可能可以为您进行演示。 Something like below should work, assuming that you were able to hide the button already: 假设您已经能够隐藏按钮,则如下所示的方法应该起作用:

  if ( divstyle.display == 'none' ) {
    divstyle.display = 'block';
  } else {
    divstyle.display = 'none';
  }

You can do it in jQuery as follows: 您可以在jQuery中执行以下操作:

$(a).click(function(){
    $(divid).toggle();
});

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

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