我想通过javascript从contentEditable div中检索文本内容.这样做有哪些选择?我尝试过innerHTML,但它不起作用.
Mir*_*cea 40
为什么不使用textContent:
var contenteditable = document.querySelector('[contenteditable]'),
text = contenteditable.textContent;
Run Code Online (Sandbox Code Playgroud)
B T*_*B T 15
不幸的是,我发现这innerText是在可疑的dom节点中保留换行符的唯一方法.我正在做什么(目前仅在Chrome中测试)是这样的:
var innerText = editableDiv.innerText // using innerText here because it preserves newlines
if(innerText[innerText.length-1] === '\n')
innerText = innerText.slice(0,-1) // get rid of weird extra newline
return innerText
Run Code Online (Sandbox Code Playgroud)
使用jQuery并做
var content = $('#my-contenteditable-div').html();
也查看这些链接:
http://west-wind.com/Weblog/posts/778165.aspx
小智 5
lblMailContent 是可编辑字段的ID.
var details= document.getElementById("lblMailContent").innerHTML;
Run Code Online (Sandbox Code Playgroud)
把这段代码放进去clientClick.它对我很有用.