为什么isContentEditable总是返回false? [英] why isContentEditable always returns me false ?

查看:133
本文介绍了为什么isContentEditable总是返回false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检查事件的退格键keyCode



I am checking the event's keyCode for backspace

var element = document.activeElement;
if (!element.isContentEditable){
    if (event.keyCode == 8) {
        return false;
    }





此代码适用于IE,但不适用于chrome,因为isContentEditable始终返回false。谁知道答案,为什么?



This code works fine with IE, but not with chrome as isContentEditable is always return false. anyone knows the answer, why?

推荐答案

这是因为你的元素永远不可编辑。就这么简单。



你需要知道两件事:

http://www.w3schools.com/jsref/prop_html_iscontenteditable.asp [ ^ ],

http ://www.w3schools.com/jsref/prop_html_contenteditable.asp [ ^ ]。



现在,有些背景



此属性与正常输入控件无关。它与属性 contentEditable 相关,它允许您编辑类似 div p 。



比较此示例中div和输入控件的行为:

This is because your element is never editable. As simple as that.

You need to know two things here:
http://www.w3schools.com/jsref/prop_html_iscontenteditable.asp[^],
http://www.w3schools.com/jsref/prop_html_contenteditable.asp[^].

Now, some background:

This property has nothing to do with "normal" input control. It is related to the attribute contentEditable which allows you to edit formatted content of something like div or p.

Compare behavior of the div and input control on this example:
<html>
    <head>
        <title>Example</title>
    </head>
<body>

<input type="text" id="text"/>
<div contentEditable="true" id="div">some content</div>

<script>
   text = document.getElementById("text");
   div = document.getElementById("div");
   alert("Editable: text: "
      + text.isContentEditable + "; div: "
      + div.isContentEditable);
</script>

</body>
</html>





首先,您将看到您可以使用文本编辑元素一些内容,即使这是常规标记。这就是HTML编辑器的创建方式。您将看到只有这个元素被认为是可编辑的。



-SA


这篇关于为什么isContentEditable总是返回false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆