admin管理员组文章数量:1426066
I found another similar questios, but almost all is for advanced things, like Android development. My question is simple, I think. I have this two codes:
function toggle(d)
{
var o=document.getElementById(d);
o.style.display=(o.style.display=='none')?'block':'none';
}
And in another file, I got that:
<a href="javascript:;" onmouseover="toggle('maisinfo');">More Info </a>
When I click on got the mouseover (second code), it just work after second try.
Anyone know where is the problem?
Obs.: The first code is in the header.php and the second on single.php (WORDPRESS)
I found another similar questios, but almost all is for advanced things, like Android development. My question is simple, I think. I have this two codes:
function toggle(d)
{
var o=document.getElementById(d);
o.style.display=(o.style.display=='none')?'block':'none';
}
And in another file, I got that:
<a href="javascript:;" onmouseover="toggle('maisinfo');">More Info </a>
When I click on got the mouseover (second code), it just work after second try.
Anyone know where is the problem?
Obs.: The first code is in the header.php and the second on single.php (WORDPRESS)
Share Improve this question asked Mar 16, 2012 at 3:43 euDenniseuDennis 3272 gold badges7 silver badges16 bronze badges 1- can you also post the html fragment with maisinfo id? – stivlo Commented Mar 16, 2012 at 3:46
3 Answers
Reset to default 4The first time, d
is set by CSS; JavaScript doesn't see that style property (See Get the Rendered Style). It initially sees o.style.display === ""
(which is not 'none'). Consequently, the first click sets it to none and the second sets it to block.
Change it to:
o.style.display = (o.style.display === 'block') ? 'none':'block';
because the first time the display property is not set therefore it is not equal to "none"
Well, there's nothing wrong with your code above. Maybe something's up with your style declaration, like setting it to block in the start which you might not want. Here's a simple JSFiddle I made for testing your code: http://jsfiddle/77DMd/1/
Hope this helps.
本文标签: wordpressJavaScript onClick work only after second clickStack Overflow
版权声明:本文标题:wordpress - JavaScript onClick work only after second click - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745469145a2659668.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论