我想检查浏览器是否不支持 SVG。如果是的话,我想在 body 中添加一个类,比如 body 中的 noSvg 。
一些论坛建议我添加 modernizer。但是,我对添加它不感兴趣,因为它很重,而且我认为这个小任务不需要它。
其他一些论坛建议我使用下面的 javascript
function supportsSVG(){
return !!('createElementNS' in document &&
document.createElementNS('http://www.w3.org/2000/svg','svg').createSVGRect)
}
我试过这个并调用了函数。但是,我不明白到底发生了什么。 任何建议将不胜感激。提前致谢。
最佳答案
试试这个,
示例 1:
if (typeof SVGRect != "undefined") {
/* If the browser does support SVG. */
} else {
/* If the browser does not support SVG. */
var bodyElement = document.getElementsByTagName("body")[0];
bodyElement.className += " noSvg";
}
示例 2:(检查 SVG 的现代方法)
if(!document.implementation.hasFeature('http://www.w3.org/TR/SVG11/feature#Image', '1.1')){
var bodyElement = document.getElementsByTagName("body")[0];
bodyElement.className += " noSvg";
}
关于javascript - 检查浏览器是否支持 SVG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35334649/