5

我需要比较来自 xml 标记属性字段的两个十六进制值,我正在尝试这个:

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

但是没有任何想法吗?

4

3 回答 3

2

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 comes 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" )
于 2012-05-12T07:59:49.457 回答
2

我认为你必须在那里使用2个等号,试试这个......

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

如果这不起作用,那么您可能没有识别 $(this)

于 2012-05-12T07:52:48.000 回答
0

如果填充是一种颜色,那么它可能会以 RGB 格式返回。当你记录它时,你会写toString(). 将其与 RGB 值进行比较或将其与字符串进行比较fill.toString(16)

于 2012-05-12T07:55:12.793 回答