【问题标题】:How to check if naturalWidth is supported?如何检查是否支持 naturalWidth?
【发布时间】:2011-10-17 12:41:51
【问题描述】:

我有以下 jQuery 并想测试是否支持 naturalWidth:

function special(image) {
    if (typeof this.naturalWidth != 'undefined') {
        //do something
    }
}

但这似乎不起作用?有什么想法吗?

【问题讨论】:

标签: jquery image height width


【解决方案1】:

试试这个

function special(image) {
    if (image && image.naturalWidth) {
        //do something
    }
}

【讨论】:

  • :) 谢谢 - 是的,当 image.naturalWidth 时似乎可以工作 :) 谢谢!
  • 在允许的情况下会这样做:) [时间限制]
【解决方案2】:

为什么不只包含一个 naturalWidth / naturalHeight polyfill? https://gist.github.com/2209957

鉴于此,您可以编写 alert($(img).naturalWidth()) 之类的代码,否则您将使用 alert(img.naturalWidth),它现在应该适用于所有浏览器,无论它们是否原生支持它。

【讨论】:

猜你喜欢
  • 2011-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多