javascript - How can I compare two hex values? - Stack Overflow

I need to pare two hex values that are ing from a xml tag attribute field, I'm trying this:var fil

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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745470430a4629107.html

相关推荐

  • javascript - How can I compare two hex values? - Stack Overflow

    I need to pare two hex values that are ing from a xml tag attribute field, I'm trying this:var fil

    1小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信