I need to pare two hex values that are ing from a xml tag attribute field, I'm trying this:
var fill = $(this).attr( "fill" );
// console.log( fill.toString(16) );
if ( fill === "#FF00FF" )
But is not working any ideas?
I need to pare two hex values that are ing from a xml tag attribute field, I'm trying this:
var fill = $(this).attr( "fill" );
// console.log( fill.toString(16) );
if ( fill === "#FF00FF" )
But is not working any ideas?
Share Improve this question asked May 12, 2012 at 7:44 Ricardo SanchezRicardo Sanchez 5,19712 gold badges61 silver badges95 bronze badges 7-
1
Isn't it redundant to do a
toString()
when jQuery'sattr
already returns a string? – Joseph Commented May 12, 2012 at 7:46 - I'm doing toString just to see what I get in the console, I'm not using the value still the if statement fails – Ricardo Sanchez Commented May 12, 2012 at 7:47
- Your code should work assuming the "fill" attribute looks like "#FF00FF", hash included. – DanRedux Commented May 12, 2012 at 7:49
-
Anyways, did you try to check what
fill
is? liketypeof fill
? did you try toconsole.log(fill)
? what does it result to? – Joseph Commented May 12, 2012 at 7:50 - it results in plain #FF00FF and other hex values – Ricardo Sanchez Commented May 12, 2012 at 7:51
3 Answers
Reset to default 1attr
returns a string, there's no need to call toString
on it (and the argument will be ignored, because String
's toString
doesn't take an argument).
Your code is assuming a couple of things:
That the attribute es back in #hex form (if it's a color value, this is not reliably true cross-browser).
That it will be in all upper case.
Not knowing what you see when you log the value, I'll just address the second part:
var fill = $(this).attr( "fill" );
if ( fill.toUpperCase() === "#FF00FF" )
I think you have to use 2 equal signs there, try this...
var fill = $(this).attr( "fill" );
if ( fill == "#FF00FF" )
If that doesn't work, then you probably not identifying $(this)
If fill is a color, then it might be returned in RGB-format. And when you log it you write toString()
. Either pare it with a RGB-value or pare it with a string as fill.toString(16)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745470430a4629107.html
评论列表(0条)