Javascript:style.display:'block'不工作 [英] Javascript: style.display:'block' doesn't work

查看:174
本文介绍了Javascript:style.display:'block'不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何想法为什么这个JavaScript只是显示阻止而不是(隐藏)DIV的内容?

any idea why this JavaScript just shows "block" and not the content of the (hidden) DIV?

<html>
<body>

<div id="mydiv" style="display:none">TEST</div>

<a href="javascript:document.getElementById('mydiv').style.display='block';">Show my DIV</a>

</body>
</html>

我也尝试过'inline'但结果相同。

I also tried 'inline' but with the same result.

return false / true也失败。

return false/true also failed.

onclick =''也失败。

onclick='' also failed.

我知道有style.visibility等,但我需要无/阻止。

I know there is style.visibility etc. but i need none/block.

此外,该函数应该在链接内部工作,我不想调用外部JS -function。

Also the function should work inside the link, i don't want to call an external JS-function.

感谢!

推荐答案

onclick事件处理程序,而不是href属性:

Because you want to use an onclick event handler, not the href attribute:

<a href="#" onclick="document.getElementById('mydiv').style.display='block';">Show my DIV</a>

jsFiddle example

jsFiddle example

(旁注:内嵌JavaScript通常是皱眉的)

(side note: inline JavaScript is usually frowned upon)

这篇关于Javascript:style.display:'block'不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆