我有以下html:

<div id="iView" name="iView" contenteditable="true" style="width: 545px; height:185px; border: 2px solid green;"><p>jgfj</p>hello this</div>
    <input type="button" value="click" onClick="getEditorText();" />

我的剧本是这样写的:
function getEditorText(){
    var viewContent = document.getElementById('iView').innerHTML;
    alert(viewContent);
    //alert(viewContent.replace(/<([^>]+)>/ig,"")); nothing works
    //alert(viewContent.replace(/(<\S([^>]+)>)/ig,"")); nothing works
    //alert(viewContent.replace(/<[^>]+>/ig,"")); nothing works
    //alert(viewContent.replace(/<\/?[^>]+(>|$)/g, "")); nothing works
    //alert(viewContent.replace(/<(?:.|\n)*?>/gm, '')); nothing works
}

在我的第一个警告中,它忽略了标签。。
但是当我动态地包含标记时..这并不是省略标记..所以让我知道如何使用javascript而不是jquery动态地剥离html标记?

最佳答案

应正常工作的代码的修改版本:

function getEditorText() {

    var el = document.getElementById('iView'),
        viewContent = el.innerText || el.textContent;

    viewContent = viewContent.replace(/<([^>]+)>/ig,"")
        .replace(/(<\S([^>]+)>)/ig,"")
        .replace(/<[^>]+>/ig,"")
        .replace(/<\/?[^>]+(>|$)/g, "")
        .replace(/<(?:.|\n)*?>/gm, '');

    el.innerHTML = viewContent;
}

演示:http://jsfiddle.net/LRCXM/2/
注意Firefox需要textContent而不是innerText

10-06 15:25