简体   繁体   English

锚标签(悬停)和 JavaScript

[英]Anchor Tag (hover) and JavaScript

The anchor tag below works.下面的锚标记有效。 However, my question is, 'how can the anchor tag force the cursor to change its appearance (like most links do) when hovering over it?'但是,我的问题是,“锚标签如何在悬停在 cursor 上时改变其外观(就像大多数链接一样)?”

<a onclick="doSomethingh(1)">Click Me</a>

Give it an HREF attribute, then disable the action when you click on the link:给它一个 HREF 属性,然后在单击链接时禁用该操作:

<a href="#" onclick="doSomethingh(1);return false;">Click Me</a>

Note, however, that it's best not to put your Javascript inline.但是请注意,最好不要将 Javascript 内联。 Use event listeners ( addEventListener / attachEvent ) instead.请改用事件侦听器 ( addEventListener / attachEvent )。

You can change the style of the cursor either inline or in a separate CSS rule.您可以内联或在单独的 CSS 规则中更改 cursor 的样式。

<a onclick="doSomethingh(1)" style="cursor: pointer">Click Me</a>

You need to give it an href.你需要给它一个href。

<a href="#" onclick="doSomethingh(1)">Click Me</a>

There are a couple of options here (the best of which is to actually put your onclick somewhere else), but this should suffice for simplicity.这里有几个选项(最好的方法是将 onclick 放在其他地方),但这应该足够简单了。

using css使用 css

a {
cursor: hand; /* show the hand like most browsers do for links */
}

Or, give the anchor a destination (href="#") and most(tm) browsers will do it for you或者,给锚点一个目的地(href="#"),大多数(tm)浏览器会为你做

more info about cursor support: http://www.quirksmode.org/css/cursor.html有关 cursor 支持的更多信息: http://www.quirksmode.org/css/cursor.html

You can do this using either JavaScript or CSS.您可以使用 JavaScript 或 CSS 来执行此操作。

CSS: CSS:

a:hover { cursor: pointer; }

JavaScript: JavaScript:

document.body.style.cursor = "pointer";

CSS would be the best practices way. CSS 将是最佳实践方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM