How to check if naturalWidth is supported?

送分小仙女□ 提交于 2019-12-10 15:48:34

问题


I have the following jQuery and wanted to test if naturalWidth is supported:

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

But this doesn't seem to work ? Any ideas ?


回答1:


Try this

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



回答2:


Why not just include a naturalWidth / naturalHeight polyfill? https://gist.github.com/2209957

Given that, you can write code like alert($(img).naturalWidth()) where you'd otherwise have used alert(img.naturalWidth) and it should now work in all browsers, whether they support it natively or not.



来源:https://stackoverflow.com/questions/6900177/how-to-check-if-naturalwidth-is-supported

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!