Javascript 将鼠标悬停的鼠标光标更改为类似锚的样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7185044/
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-24 01:14:47  来源:igfitidea点击:

Change the mouse cursor on mouse over to anchor-like style

javascriptcssmouse

提问by shibly

If I hover the mouse over a divthe mouse cursor will be changed to the cursor like that in HTML anchor.

如果我将鼠标悬停在 a 上div,鼠标光标将更改为 HTML 锚点中的光标。

How can I do this? Do I need Javascript or it's possible with CSS only?

我怎样才能做到这一点?我需要 Javascript 还是只需要 CSS 就可以?

回答by Devin Burke

Assuming your divhas an id="myDiv", add the following to your CSS. The cursor: pointerspecifies that the cursor should be the same hand icon that is use for anchors (hyperlinks):

假设您div有一个id="myDiv",请将以下内容添加到您的 CSS 中。的cursor: pointer指定光标应该是相同的手的图标是用于使用锚(超链接):

CSS to Add

要添加的 CSS

#myDiv
{
    cursor: pointer;
}

You can simply add the cursor style to your div's HTML like this:

您可以简单地将光标样式添加到您div的 HTML 中,如下所示:

<div style="cursor: pointer">

</div>

EDIT:

编辑:

If you are determined to use jQuery for this, then add the following line to your $(document).ready()or body onload: (replace myClasswith whatever class all of your divs share)

如果您决定为此使用 jQuery,请将以下行添加到您的$(document).ready()或正文中onload:(替换myClass为您div共享的任何类)

$('.myClass').css('cursor', 'pointer');

回答by Ryan Atallah

If you want to do this in jQuery instead of CSS, you basically follow the same process.

如果你想在 jQuery 而不是 CSS 中做到这一点,你基本上遵循相同的过程。

Assuming you have some <div id="target"></div>, you can use the following code:

假设您有一些<div id="target"></div>,您可以使用以下代码:

$("#target").hover(function() {
    $(this).css('cursor','pointer');
}, function() {
    $(this).css('cursor','auto');
});

and that should do it.

那应该这样做。

回答by attack

You actually don't need jQuery, just CSS. For example, here's some HTML:

您实际上不需要 jQuery,只需要 CSS。例如,这里有一些 HTML:

<div class="special"></div>

And here's the CSS:

这是CSS:

.special
{
    cursor: pointer;
}

回答by Sinetheta

This will

这会

#myDiv
{
    cursor: pointer;
}

回答by being_ethereal

I think :hoverwas missing in above answers. So following would do the needful.(if css was required)

我认为:hover上面的答案中缺少。因此,以下将做必要的事情。(如果需要 css)

#myDiv:hover
{
    cursor: pointer;
}