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'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)
本文标签: javascriptHow can I compare two hex valuesStack Overflow
版权声明:本文标题:javascript - How can I compare two hex values? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745470430a2659723.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论