type in empty divs while contenteditable = true?

纵然是瞬间 提交于 2019-12-23 05:29:06

问题


why won't the cursor enter some of my divs when contenteditable is enabled?

also, why does my browser tell me there is nothing in the div when there is clearly a space character in it?!

var div = '<div> </div>'; 
div.innerHTML = undefined 

What gives?


回答1:


contenteditable does not apply to empty elements, so you may want to insert a non-breaking-space character into the empty elements you want to be able to edit

the non-breaking-space "&nbsp;" will act as a character when left on its own,
whereas a whitespace " " will only count as a character if placed after another character

    <div>" "</div> ----------------> innerHTML = undefined
    <div>"&nbsp;"</div> --------> innerHTML =


the gray box around &nbsp; means its an html character
so make sure you insert a "&nbsp" and not a " "

var nbsp = "&nbsp;",
    div = document.getElementById('id');
div.innerHTML = nbsp

jquery works too

var div = $('div#id')
div.html(nbsp1)


来源:https://stackoverflow.com/questions/22028406/type-in-empty-divs-while-contenteditable-true

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