您将如何编写一个将元素onmouseover / onmouseout的显示属性从display:none切换到display:block;的切换,或者不管如何,jquery的切换可以操作的任何css属性-您如何精确地编写没有jquery的切换功能?
为什么不能在以下代码中将javascript css属性作为 bool(boolean) 值?
<html>
<head>
<style type="text/css">
a {
display:none;
}
</style>
<script type="text/javascript">
function ab(a)
{
if(document.getElementById(a).style.display=='none')
{
document.getElementById(a).style.display="block";
}
else if(document.getElementById(a).style.display=="block")
{
document.getElementById(a).style.display="none";
}
}
</script>
</head>
<body>
<div class="fg">
<div style="width:100px;height:100px;border:2px solid blue;" onmouseover="ab('g');"
onmouseout="ab('g');">
<a id="g" href="http://www.dfadsafafa.com">lajflke</a></div></div>
</body>
</html>
最佳答案
您提到的解决方案看起来不错,但可以使其更简洁。
function toggle(id){
var div1 = document.getElementById(id);
if (div1.style.display == 'none') {
div1.style.display = 'block'
} else {
div1.style.display = 'none'
}
}