admin管理员组

文章数量:1426005

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's attr 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? like typeof fill? did you try to console.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
 |  Show 2 more ments

3 Answers 3

Reset to default 1

attr 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:

  1. That the attribute es back in #hex form (if it's a color value, this is not reliably true cross-browser).

  2. 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)

本文标签: javascriptHow can I compare two hex valuesStack Overflow