【问题标题】:To check whether the css3 and/or HTML5 property is supported by browser using JavaScript [duplicate]使用 JavaScript 检查浏览器是否支持 css3 和/或 HTML5 属性 [重复]
【发布时间】:2013-02-16 17:45:20
【问题描述】:

我想检查用户浏览器是否支持 "contenteditable" 或任何 HTML5 属性

这是我的 JavaScript:

var isEditable=false;
function chk() {
    var z=document.getElementById("mydivid");

    if(typeof(z["isContentEditable"])==="boolean") {
        isEditable=true;
    }
}

function doEdit() {
    chk();
    var z=document.getElementById("mydivid");
    if(isEditable) {
        z.setAttribute("contenteditable","true");
        z.focus();
    } else {
        /* add a texbox and put all div's innerHTML into it, All in all:  a Boring Stuff. */
    }
}

这是 HTML:

<a href="#" onclick="doEdit();">Click To Edit </a>
<div id="mydivid"> Hi Ssup ?? <img src='' alt="some image" /></div>

谁能告诉我我的方法是否正确?它可以在 IE(version

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    简单检查:

    if ('isContentEditable' in document.createElement('span')) {
        // supported
    }
    

    我还将分享这个精选的 sn-ps,它将在未来帮助您:http://diveintohtml5.info/everything.html

    【讨论】:

    • 感谢分享这么好的信息!
    【解决方案2】:

    检查元素是否存在任何属性。你可以这样做

    var element = document.createElement('__ELEMENT__');
    if ('__PROPERTY__' in element ) {
        // property supported in the browser
    }
    

    if ('__PROPERTY__' in document.createElement('__ELEMENT__') ) {
        // property supported in the browser
    }
    

    以下链接包含所有内容。

    1. https://github.com/Modernizr/Modernizr/issues/570
    2. http://diveintohtml5.info/everything.html#contenteditable

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-23
      • 2020-01-04
      • 2011-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多