jQuery获取文本阴影可变 [英] jQuery getting text-shadow variabile
问题描述
我想要获得4个变量,当我点击一个跨度的CSS3文本阴影妥善。因此,对于文本阴影的CSS礼:-4px 11像素RGB 8像素(30,43,2);
,我的代码应该是:
I want to get 4 variables when I click on a span that have the CSS3 text-shadow propriety. So for a css propriety of text-shadow: -4px 11px 8px rgb(30, 43, 2);
, my code should be:
$("#element").click(function () {
var text-shadow = $("#element").css("text-shadow")
});
可以将它拆分为:
var y = "-4px";
var x = "11px";
var blur = "8px";
color = "rgb(30, 43, 2)";
我需要以某种方式拆分第一个变量来获取这些数据。
I need to somehow split the first variable to get this data.
感谢名单
推荐答案
您应该使用正则表达式的jQuery CSS的结果分为您是变量寻找。
You should use regular expression to split the result of jQuery css into the variables you are looking for.
var result = $('#element').css('text-shadow').match(/(-?\d+px)|(rgb\(.+\))/g)
// result => ['rgb(30, 43, 2)', '-4px', '11px', '8px']
var color = result[0],
y = result[1],
x = result[2],
blur = result[3];
这将返回一个分割 text-shadow
字符串值转换为带有像素和rgb值的数字。它可以帮助你在这种特殊情况下,但你可能需要做一些工作,以使它工作在所有可能的情况下 text-shadow
This will return an array splitting the text-shadow
string value into numbers with pixels and rgb values. It can help you in this particular case, but you'll probably need to work on it some more to get it working for all the possible cases of text-shadow
注意: rgb(...)
值是数组中的第一个匹配项,因为,这是Firefox,Chrome,Safari和Opera返回的方式,独立于你如何分配它。 IE可能会有所不同。
NOTE: The rgb(...)
value is the first match in the array because, that is the way Firefox, Chrome, Safari and Opera return it, independently of how you assign it. IE might do it differently.
这篇关于jQuery获取文本阴影可变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!