Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

css - jQuery getting text-shadow variabile

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")
});

Would it be possible to get it split like:

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.

Thanx

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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];

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...