我需要比较来自 xml 标记属性字段的两个十六进制值,我正在尝试这个:
var fill = $(this).attr( "fill" );
// console.log( fill.toString(16) );
if ( fill === "#FF00FF" )
但是没有任何想法吗?
我需要比较来自 xml 标记属性字段的两个十六进制值,我正在尝试这个:
var fill = $(this).attr( "fill" );
// console.log( fill.toString(16) );
if ( fill === "#FF00FF" )
但是没有任何想法吗?
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:
That the attribute comes 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" )
我认为你必须在那里使用2个等号,试试这个......
var fill = $(this).attr( "fill" );
if ( fill == "#FF00FF" )
如果这不起作用,那么您可能没有识别 $(this)
如果填充是一种颜色,那么它可能会以 RGB 格式返回。当你记录它时,你会写toString()
. 将其与 RGB 值进行比较或将其与字符串进行比较fill.toString(16)