简体   繁体   English

如何比较两个十六进制值?

[英]How can I compare two hex values?

I need to compare two hex values that are coming from a xml tag attribute field, I'm trying this: 我需要比较来自xml标记属性字段的两个十六进制值,我正在尝试这样做:

var fill = $(this).attr( "fill" );
// console.log( fill.toString(16) );
if ( fill === "#FF00FF" )

But is not working any ideas? 但是没有任何想法吗?

I think you have to use 2 equal signs there, try this... 我想你必须在那里使用2个等号,试试这个......

var fill = $(this).attr( "fill" );
if ( fill == "#FF00FF" )

If that doesn't work, then you probably not identifying $(this) 如果这不起作用,那么你可能不会识别$(this)

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). attr返回一个字符串,不需要在其上调用toString (并且该参数将被忽略,因为StringtoString不接受参数)。

Your code is assuming a couple of things: 您的代码假设了以下几点:

  1. That the attribute comes back in #hex form (if it's a color value, this is not reliably true cross-browser). 该属性以#hex形式返回(如果它是一个颜色值,这不是真正的跨浏览器)。

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

If fill is a color, then it might be returned in RGB-format. 如果fill是一种颜色,则它可能以RGB格式返回。 And when you log it you write toString() . 当你记录它时,你写toString() Either compare it with a RGB-value or compare it with a string as fill.toString(16) 要么将它与RGB值进行比较,要么将其与字符串作为fill.toString(16)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM