[英]Apply css settings to all clickable elements
I want to apply cursor: pointer;
我想应用
cursor: pointer;
to any element that has jQuery click()
function bound to it. 绑定到任何具有jQuery
click()
函数的元素。
How can I do this without going to each element in my CSS file and manually adding cursor: pointer;
我如何做到这一点而无需转到CSS文件中的每个元素并手动添加
cursor: pointer;
to it? 要吗? (I have a lot of various clickable items)
(我有很多可点击的项目)
<span class = "clickable1">something</span>
<span class = "clickable2">something</span>
<span class = "clickable3">something</span>
$(".clickable1").click(function(){
//do something
});
$(".clickable2").click(function(){
//do something
});
$(".clickable3").click(function(){
//do something
});
You actually can do this. 您实际上可以做到这一点。
By checking the internal $._data
you can get the bound events on any element, then it's just a matter of checking if click
is one of those events and attaching the style etc. 通过检查内部的
$._data
您可以获取任何元素上的绑定事件,然后只需检查click
是否是这些事件之一并附加样式等即可。
$('*').filter(function() {
var d = $._data( this, "events" );
return d && 'click' in d;
}).css('cursor', 'pointer');
Add another class called isclickable
so you would have 添加另一个名为
isclickable
类,这样您就可以
<span class = "isclickable clickable1">something</span>
<span class = "isclickable clickable2">something</span>
<span class = "isclickable clickable3">something</span>
Then you can have .isclickable { cursor: pointer; }
然后,您可以使用
.isclickable { cursor: pointer; }
.isclickable { cursor: pointer; }
in your css .isclickable { cursor: pointer; }
在你的CSS
Solution 1: give every clickable element the same class like 解决方案1:为每个可点击元素提供相同的类
<span class = "clickable clickable1">something</span>
<span class = "clickable clickable2">something</span>
<span class = "clickable clickable3">something</span>
Solution 2: expand jQuery selector $(".clickable1, .clickable2, .clickable3")
解决方案2:展开jQuery选择器
$(".clickable1, .clickable2, .clickable3")
Why not add a common class to all of them? 为什么不为所有这些添加通用类?
<span class = "clickable clickable1">something</span>
<span class = "clickable clickable2">something</span>
<span class = "clickable clickable3">something</span>
Then, with css: 然后,使用CSS:
.clickable {
cursor: pointer;
}
If you'd rather not change the HTML you already have manually (or if you can't), you can add the class when binding the event handlers: 如果您不希望手动更改(或者如果不能)更改已经拥有的HTML,则可以在绑定事件处理程序时添加该类:
function addClickBehavior($el, callback) {
return $el.addClass("clickable").on("click", callback);
}
addClickBehavior($(".clickable1"), function(){
//do something
});
addClickBehavior($(".clickable2"), function(){
//do something
});
addClickBehavior($(".clickable3"), function(){
//do something
});
Simply use a selector that selects all elements whose class name starts with "clickable": 只需使用选择器即可选择其类名以“ clickable”开头的所有元素:
$("*[class^='clickable']").css('cursor', 'pointer').click(function(){
//do something
});
BTW: Seems like a very "inefficient" use of the class attribute and class names aren't very "useful". 顺便说一句:似乎非常“低效”地使用了class属性,而类名并不是非常“有用”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.