contentEditable div allow only TEXT and IMG tag
If I have an contentEditable
div can I remove all HTML elements except for IMG
with specific class. I asked 开发者_如何学运维a question about that, but I have troubles with it. The problem is that Google Chrome uses div
s as lines break / separator, Internet Explorer uses p
, and the Mozilla Firefox uses only <br>
.
How can I remove all HTML elements Except for Images from the contentEditable
div, and keep lines, for all browsers?
I used :
var element = document.getElementById("myDiv");
for(var i=0; i<element.children.length;i++)
{
if(element.children[i].tagName != "IMG" ||
element.children[i].className != "requiredClassName")
{
element.insertBefore(document.createTextNode(element.children[i].innerHTML.replace(/<\/?.*?\/?>/gi, ''), element.children[i]);
element.children[i].parentNode.removeChild(element.children[i]);
}
}
But it causes troubles in IE and Chrome, it removes all lines, and all text became in one long line.
I need only pure JavaScript code.
精彩评论