jquery:删除指针光标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17760026/
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
jquery: remove the pointer cursor?
提问by laukok
Why can't I change the CSS of an a
tag with jquery? For instance,
为什么我不能a
使用 jquery更改标签的 CSS ?例如,
html,
html,
<a href="#">1</a>
<a href="">2</a>
<a href="http://website.com/#/home/about/">3</a>
<a href="http://website.com/home/about/">4</a>
link 1 and 2 are not clickable so I want to remove the pointer cursor.
链接 1 和 2 不可点击,所以我想删除指针光标。
jquery,
jQuery,
$("a").click(function(e){
if($(this).attr("href") == "#" || $(this).attr("href") == "") {
alert("this is non-clickable");
$(this).css({cursor:"default"});
e.preventDefault();
}
else{
alert($(this).attr("href"));
$(this).css({cursor:"pointer"});
e.preventDefault();
}
});
is it possible?
是否可以?
回答by Mr. Alien
If you want you can simply do this with the CSS
如果你愿意,你可以简单地用 CSS 来做到这一点
a[href="\#"], a[href=""] {
cursor: default;
}
/* I've used an element[attr] selector here which will select all a tags
with this combination, if you want to target specific a tags,
wrap them in an element with a class and you can than refer as
.class a[href]... */
If you want to disable the links, you can achieve that too using CSS pointer-events: none;
property
如果你想禁用链接,你也可以使用 CSSpointer-events: none;
属性来实现
Demo 2(Will help you if JS is disabled, this won't alert the message, but it will help you to disable the event which you are trying to do)
演示 2(如果 JS 被禁用会帮助你,这不会提醒消息,但它会帮助你禁用你试图做的事件)
回答by laukok
Your error is in your jquery css. You need quotes round css your js should look like this:
您的错误在您的 jquery css 中。你需要在 css 周围引用你的 js 应该是这样的:
$("a").click(function(e){
if($(this).attr("href") == "#" || $(this).attr("href") == "") {
alert("this is non-clickable");
$(this).css({'cursor' :"default"});
e.preventDefault();
}
else{
alert($(this).attr("href"));
$(this).css({'cursor':"pointer"});
e.preventDefault();
}
});
Also you could use addClass method then in the css have a style for no href
你也可以使用 addClass 方法,然后在 css 中有一个没有 href 的样式
回答by ABorty
try to change the line
尝试改变行
form
形式
$(this).css({cursor:"default"});
to
到
$(this).css('cursor','default');
let me know if you face any problem
如果您遇到任何问题,请告诉我