如何将光标样式设置为没有hrefs的链接的指针

kev*_*vin 127 html javascript css cursor hyperlink

我有很多<a>没有href用于进行onclickjavascript调用的属性的html标签.这些链接没有光标的指针样式.它们有文本样式的光标.

如何在不使用href属性的情况下将光标样式设置为链接的指针?

我知道我可以添加href ="#".我在html文档中的很多地方都有这个,并且想知道如何在不使用href属性的情况下为链接创建游标样式指针.

Ros*_*oss 202

在你的css文件中添加这个....

a:hover {
 cursor:pointer;
}
Run Code Online (Sandbox Code Playgroud)

如果您没有css文件,请将其添加到HTML页面的HEAD中

<style type="text/css">
 a:hover {
  cursor:pointer;
 }
</style>
Run Code Online (Sandbox Code Playgroud)

你也可以在你的javascript结尾处返回false来使用href =""属性.

<a href="" onclick="doSomething(); return false;">a link</a>
Run Code Online (Sandbox Code Playgroud)

这有很多原因.搜索引擎优化或如果人们没有javascript,href =""将起作用.例如

<a href="nojavascriptpage.html" onclick="doSomething(); return false;">a link</a>
Run Code Online (Sandbox Code Playgroud)

@see http://www.alistapart.com/articles/behavioralseparation

编辑:值得注意的是@BalusC的回答他提到的答案:hover对于OP的用例来说并不是必需的.虽然可以使用:hover选择器添加其他样式.

  • 使用:悬停不是必需的,并且不保证在没有href的链接上工作. (5认同)

Bal*_*usC 15

只需将其添加到您的全局CSS样式:

a { cursor: pointer; }
Run Code Online (Sandbox Code Playgroud)

这样您就不再依赖于浏览器默认光标样式了.


mxm*_*ile 8

style ="cursor:pointer;"


And*_*lam 7

cursor: pointer如果我没记错的话,使用CSS 。

在您的 CSS 文件中:

.link_cursor
{
    cursor: pointer;
}
Run Code Online (Sandbox Code Playgroud)

然后只需将以下 HTML 添加到您想要拥有链接光标的任何元素:(class="link_cursor"首选方法。)

或者使用内联CSS:

<a style="cursor: pointer;">
Run Code Online (Sandbox Code Playgroud)


小智 6

将鼠标悬停在给定object(myObject)上时,这就是将光标从箭头更改为手的方式。

myObject.style.cursor = 'pointer';
Run Code Online (Sandbox Code Playgroud)