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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2409836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:12:02  来源:igfitidea点击:

how to set cursor style to pointer for links without hrefs

javascripthtmlcsshyperlinkcursor

提问by kevin

I have a lot of <a>html tags without the hrefattribute for making onclickjavascript calls. These links do not have a pointer style of cursor. They have text style cursor.

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

How can I set the cursor style to pointer for links without using the hrefattribute?

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

I know I can add href="#". I have this in a lot of places in the html document, and would like to know how to make cursor style pointer for links without using the hrefattribute.

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

回答by Ross

in your css file add this....

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

a:hover {
 cursor:pointer;
}

if you don't have a css file, add this to the HEAD of your HTML page

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

<style type="text/css">
 a:hover {
  cursor:pointer;
 }
</style>

also you can use the href="" attribute by returning false at the end of your javascript.

您也可以通过在 javascript 末尾返回 false 来使用 href="" 属性。

<a href="" onclick="doSomething(); return false;">a link</a>

this is good for many reasons. SEO or if people don't have javascript, the href="" will work. e.g.

这有很多好处。SEO 或者如果人们没有 javascript,href="" 将起作用。例如

<a href="nojavascriptpage.html" onclick="doSomething(); return false;">a link</a>

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

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

Edit: Worth noting @BalusC's answerwhere he mentions :hoveris not necessary for the OP's use case. Although other style can be add with the :hoverselector.

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

回答by BalusC

Just add this to your global CSS style:

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

a { cursor: pointer; }

This way you're not dependent on the browser default cursor style anymore.

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

回答by mxmissile

style="cursor: pointer;"

样式=“光标:指针;”

回答by Lou

This is how change the cursor from an arrow to a hand when you hover over a given object(myObject).

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

myObject.style.cursor = 'pointer';

回答by Alxandr

Give them all a common class (for instance link). Then add in css-file:

给他们一个共同的类(例如链接)。然后在css文件中添加:

.link { cursor: pointer; }

Or as @mxmissile suggested, do it inline with style="cursor: pointer;"

或者按照@mxmissile 的建议,使用 style="cursor: pointer;" 内联

回答by Dan

create a class with the following CSS and add it to your tags with onclick events:

使用以下 CSS 创建一个类,并使用 onclick 事件将其添加到您的标签中:

cursor:pointer;

回答by Andy Shellam

Use CSS cursor: pointerif I remember correctly.

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

Either in your CSS file:

在您的 CSS 文件中:

.link_cursor
{
    cursor: pointer;
}

Then just add the following HTML to any elements you want to have the link cursor: class="link_cursor"(the preferred method.)

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

Or use inline CSS:

或者使用内联 CSS:

<a style="cursor: pointer;">

回答by chachan

Angular version:

角度版本:

<a (click)="action(); $event.preventDefault()" href="#">Action</a>

回答by BabaRick

This worked for me:

这对我有用:

<a onClick={this.openPopupbox} style={{cursor: 'pointer'}}>