简体   繁体   中英

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?'

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

Give it an HREF attribute, then disable the action when you click on the link:

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

Note, however, that it's best not to put your Javascript inline. Use event listeners ( addEventListener / attachEvent ) instead.

You can change the style of the cursor either inline or in a separate CSS rule.

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

You need to give it an 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.

using 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

more info about cursor support: http://www.quirksmode.org/css/cursor.html

You can do this using either JavaScript or CSS.

CSS:

a:hover { cursor: pointer; }

JavaScript:

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

CSS would be the best practices way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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