开发者

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 divs 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜