如何告诉Chrome浏览器在使用CSSStyleDeclaration.setProperty()时不要将电话号码转换为



当我在Chrome中使用以下行时,该值将被转换然后保存,但不应转换。Firefox 不会这样做。

这是为了将一些数据作为CSS变量保存到a。

document.documentElement.style.setProperty("--phone", "+49 7761 9935370")
const result = document.documentElement.style.getPropertyValue("--phone")
console.log(result)

我期望输出"+49 7761 9935370"(在 Firefox 中是这样(,但 Chrome 中的实际输出是"49 7761 9.93537e+6"。

为什么不使用setAttribute

document.documentElement.setAttribute('data-phone','+49 7761 9935370');
var phone = document.documentElement.getAttribute('data-phone');

您是否尝试过在分配的值周围使用引号

document.documentElement.style.setProperty("--phone", "'+49 7761 9935370'")

在真正的 CSS 规则中,您也不会在没有引号的情况下编写此变量内容(没有意义,因为该值包含空格( - 那么为什么现在在通过 JS 分配相同的值时省略它们......?

最新更新