Javascript 比较getComputedStyle()宽度值的函数返回意外的真值

Javascript 比较getComputedStyle()宽度值的函数返回意外的真值,javascript,Javascript,我有以下功能 const wider_than = (el1, el2) => getComputedStyle(el1).width > getComputedStyle(el2).width; 我使用它来控制嵌套div的innerhtml值。如果inner div比outter div宽,我希望inner div将其文本设置为“error”。否则我想将其设置为用户定义的值 但函数在意外情况下返回true。使用以下日志 console.log(getComputedStyle(d

我有以下功能

const wider_than = (el1, el2) => getComputedStyle(el1).width > getComputedStyle(el2).width;
我使用它来控制嵌套div的innerhtml值。如果inner div比outter div宽,我希望inner div将其文本设置为“error”。否则我想将其设置为用户定义的值

但函数在意外情况下返回true。使用以下日志

console.log(getComputedStyle(display_text).width);
console.log(getComputedStyle(display).width);
console.log(wider_than(display_text, display));
console.log(getComputedStyle(display_text).width > getComputedStyle(display).width);
并逐个字符更新显示文本innerHTML的值,第一个字符正常运行,但第二个字符中断。下面是日志的输出

53.3958px
639px
false
false
80.0625px
639px
true
true
有人能解释为什么会发生这种情况吗?

console.log('b'>a:','b'>a”)//“b”在“a”之后
console.log(‘“bun”>“abbot”:’,“bun”>“abbot”)//无论字长如何,“b”在“a”之后
console.log(““2”>“1”:“,“2”>“1”);

console.log(“'2”>“100”:“,“2”>“100”)您正在比较字符串。从词典学上看,
“8”
“6”
多,因此结果是。在比较之前,您需要将这些值
解析为数字,这应该是从输出中的px中知道的。非常感谢。@isherwood我正要睡觉,所以我没有时间发布完整的答案。我原以为现在会有人来,但因为什么都没有,我就发了。