我正在使用 Javascript 绘制一个 div,并在构建它时向其添加各种属性。我可以获得 ids、classes 和 style 属性,但它只是忽略了“contenteditable”。
var elemText = document.createElement('div');
elemText.className = 'elem';
elemText.style.background = "none";
elemText.id = "elementID";
elemText.contenteditable = "true";
我也试过
elemText.attributes['contenteditable'] = "true";
仍然没有快乐。
最佳答案
属性是contentEditable
(注意大写字母“E”)。使用 setAttribute()
设置属性而不是 attributes
收藏。因此,以下任一方法都有效:
elemText.contentEditable = "true";
elemText.setAttribute("contenteditable", "true");
关于javascript - 使用 Javascript 向元素添加 "contenteditable"属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20906895/