wordpress - JavaScript onClick work only after second click - Stack Overflow

I found another similar questios, but almost all is for advanced things, like Android development. My q

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
Add a ment  | 

3 Answers 3

Reset to default 4

The 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745469146a4629052.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信