开发者

contentEditable div allow only TEXT and IMG tag

开发者 https://www.devze.com 2023-04-04 15:50 出处:网络
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 Goo

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

精彩评论

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