当用户将鼠标悬停在列表项上时,如何将光标变为手形?
我有一个列表,并且有一个用于其项目的单击处理程序:
<ul>
<li>foo</li>
<li>goo</li>
</ul>
如何将鼠标指针更改为手形指针(例如将鼠标悬停在按钮上时)?现在,当我将鼠标悬停在列表项上时,指针会变成文本选择指针。
I've got a list, and I have a click handler for its items:
<ul>
<li>foo</li>
<li>goo</li>
</ul>
How can I change the mouse pointer into a hand pointer (like when hovering over a button)? Right now the pointer turns into a text selection pointer when I hover over the list items.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(28)
随着时间的推移,正如人们所提到的,您现在可以安全地使用:
In light of the passage of time, as people have mentioned, you can now safely just use:
用于
li
:运行代码片段选项后查看更多光标属性和示例:
Use for
li
:See more cursor properties with examples after running snippet option:
为此,您不需要 jQuery,只需使用以下 CSS 内容:
瞧!便利。
You do not require jQuery for this, simply use the following CSS content:
And voilà! Handy.
使用:
当前 HTML 规范的其他有效值(
hand
不是)可以查看 此处。Use:
Other valid values (which
hand
is not) for the current HTML specification can be viewed here.请使用!
如果您想获得跨浏览器结果,
Use
if you want to have a crossbrowser result!
CSS:
您还可以让光标成为图像:
CSS:
You can also have the cursor be an image:
我认为当 JavaScript 可用时仅显示手形/指针光标是明智的做法。所以人们不会有可以点击不可点击的东西的感觉。
为了实现这一点,您可以使用 JavaScript 库 jQuery 将 CSS 添加到元素,如下所示,
或者将其直接链接到单击处理程序。
或者当 modernizer 与
结合使用时使用时,CSS 看起来像这样:
I think it would be smart to only show the hand/pointer cursor when JavaScript is available. So people will not have the feeling they can click on something that is not clickable.
To achieve that you could use the JavaScript libary jQuery to add the CSS to the element like so
Or chain it directly to the click handler.
Or when modernizer in combination with
<html class="no-js">
is used, the CSS would look like this:对于完整的跨浏览器,请使用:
For complete cross browser, use:
只是为了完整起见:
它还提供了一只手,就是您在移动图像视图时所知道的那只手。
如果您想使用 jQuery 和 mousedown 来模拟抓取行为,它会非常有用。
Just for completeness:
It also gives a hand, the one you know when moving the view of an image around.
It is quite useful if you want to emulate grab behavior using jQuery and mousedown.
您可以在悬停时更改它,也可以仅在列表项上指定
cursor:pointer
,两者都可以。或者
You can change it either on hover or just specify
cursor:pointer
on list item, both will work.Alternatively
简单来说就是这段代码。
Simply put this code.
为了能够使任何内容得到“mousechange”处理,您可以添加一个 CSS 类:
我不会说使用
cursor:hand
因为它仅对 Internet Explorer 5.5 及更低版本有效,并且 Internet Explorer 6 随 Windows XP (2002) 一起提供。只有当浏览器停止工作时,人们才会收到升级提示。此外,在 Visual Studio 中,该条目将以红色下划线显示。它告诉我:For being able to make anything get the "mousechange" treatment, you can add a CSS class:
I would not say to use
cursor:hand
since it was only valid for Internet Explorer 5.5 and below, and Internet Explorer 6 came with Windows XP (2002). People will only get the hint to upgrade when their browser stops working for them. Additionally, in Visual Studio, it will red underline that entry. It tells me:只需执行以下操作:
我将其应用到您的代码上,看看它是如何工作的:
注意:另外不要忘记您可以拥有任何带有自定义光标的手形光标,您可以创建最喜欢的手形图标,例如:
Simply just do something like this:
I apply it on your code to see how it works:
Note: Also DO not forget you can have any hand cursor with customised cursor, you can create fav hand icon like this one for example:
所有其他响应都建议使用标准 CSS 指针,但是有两种方法:
将 CSS 属性
cursor:pointer;
应用于元素。 (这是当光标悬停在按钮上时的默认样式。)使用指针的自定义图形应用 CSS 属性
cursor:url(pointer.png);
。如果您想确保所有平台上的用户体验都相同(而不是让浏览器/操作系统决定您的指针光标应该是什么样子),这可能更理想。请注意,如果找不到图像,可能会添加后备选项,包括辅助 url 或任何其他选项,即cursor:url(pointer.png,fallback.png,pointer);
当然这些可以以这种方式应用于列表项
li{cursor:pointer;}
,作为类.class{cursor:pointer;}
,或者作为每个元素的样式属性style="cursor:pointer;"
。All of the other responses suggest using the standard CSS pointer, however, there are two methods:
Apply the CSS property
cursor:pointer;
to the elements. (This is the default style when a cursor hovers over a button.)Apply the CSS property
cursor:url(pointer.png);
using a custom graphic for your pointer. This may be more desirable if you want to ensure that the user experience is identical on all platforms (instead of allowing the browser/OS decide what your pointer cursor should look like). Note that fallback options may be added in case the image is not found, including secondary urls or any of the other options i.e.cursor:url(pointer.png,fallback.png,pointer);
Of course these may be applied to the list items in this manner
li{cursor:pointer;}
, as a class.class{cursor:pointer;}
, or as a value for the style attribute of each elementstyle="cursor:pointer;"
.使用:
有关更多鼠标事件,请检查 CSS 光标属性。
Use:
For more mouse events, check CSS cursor property.
您可以使用以下其中一项:
或
工作示例 1:
工作示例2:
You can use one of the following:
or
Working example 1:
Working example 2:
对于基本的手形符号:
尝试
如果您想要一个手形符号,例如拖动某个项目并将其放下,请尝试:
For a basic hand symbol:
Try
If you want a hand symbol like drag some item and drop it, try:
您可以使用下面的代码:
li:hover {cursor:pointer; }
You can use the code below:
li:hover { cursor: pointer; }
检查以下内容。我从 W3Schools 获取它。
Check the following. I get it from W3Schools.
您只需使用 CSS 样式即可。
You can just use CSS style for this.
CSS
引导程序
css
bootsrap
使用 HTML Hack
注意:不建议这样做,因为这被认为是不好的做法
将内容包装在包含
href
属性的锚标记中将起作用没有显式应用具有锚点属性副作用的cursor:pointer;
属性(用 CSS 修改):Using an HTML Hack
Note: this is not recommended as it is considered bad practice
Wrapping the content in an anchor tag containing an
href
attribute will work without explicitly applying thecursor: pointer;
property with the side effect of anchor properties (amended with CSS):您还可以使用以下样式:
You can also use the following style:
只是使用CSS来设置自定义光标指针
演示
例如
.cur、.png、.svg、.jpeg、.webp
等参考文献
https://developer.mozilla.org/en-US/docs /Web/CSS/光标
just using CSS to set customize the cursor pointer
demo
such as
.cur, .png, .svg, .jpeg, .webp
, and so onrefs
https://developer.mozilla.org/en-US/docs/Web/CSS/cursor
tailwindcss 中的等效类是“cursor-pointer”。
it's equivalent class in tailwindcss is `cursor-pointer'.
CSS 中有多种类型的光标:
https://www. w3schools.com/cssref/tryit.asp?filename=trycss_cursor
There are several types of cursors available in CSS:
https://www.w3schools.com/cssref/tryit.asp?filename=trycss_cursor