我有这个:
<div id="click"><a href="link.html"><img src="img/image.png" alt="something"></a>
<a
onmouseover="ReverseContentDisplay('cont'); return true;"
href="javascript:ReverseContentDisplay('cont')">
info
</a>
</div>
<div id="cont"></div>现在,当我将鼠标移到显示“信息”的文本上时,<div id="cont"></div>就会出现。但是,我现在需要更改标记,以便锚标记具有链接(而不是cont函数),并在鼠标悬停时显示javascript div。
发布于 2011-04-20 03:13:55
Here is an example of what you may want to do。它通过从HTML中移除JS来提供不起眼的Javascript。HTML是:
<div id="click"><a href="link.html"><img src="img/image.png" alt="something"></a>
<!-- the href attribute below should have whatever link you want -->
<a id="showCont" href="http://www.google.com">info</a>
</div>
<div id="cont">Cont</div>javascript将位于一个单独的文件中,该文件从<script>标记引用,并且是:
window.onload = init; // call init on load
function init() {
document.getElementById("showCont").onmouseover = function() { ReverseContentDisplay("cont") }; // set up the onmouseover handler
}
function ReverseContentDisplay(id) {
var elem = document.getElementById(id);
if(elem.style.display === "block") {
elem.style.display = "none";
} else {
elem.style.display = "block";
}
}CSS也会在一个单独的文件中,下面是用来隐藏cont div的:
#cont {
display:none;
}发布于 2011-04-20 03:10:44
我不确定我是否理解了问题所在,但是如果您想保持鼠标悬停并保持有效链接,只需将URL放在href属性中即可。
其他提示: 1.你所做的不是语义上的。希望将标记与行为或样式分开。您应该为单击JavaScript中的链接添加一个事件侦听器。2.记住渐进式增强,并不是每个人都打开了JavaScript。3.为什么在onmouseover中返回true?如果它有目的(虽然我看不出来),你应该在函数中返回true。
https://stackoverflow.com/questions/5721262
复制相似问题