How to handle wysihtml5`s br and nbsp?

戏子无情 提交于 2019-12-11 08:55:58

问题


I'm using wysihtml5 editor and having some problems with the output.

When I hit return I get a line break which is OK, but the tag isn't valid XHTML. It uses <br> instead of <br />.

The other problem I experienced is when I create two spaces. I then get the &nbsp; element, which makes my XML parser fail..

Anyone know how to remove these from occurring?


回答1:


The following codes provide text content(without html tags) and html content(with html tags). Who wants which of them;

textContent = document.getElementsByClassName('wysihtml5-sandbox')[0].contentWindow.document.body.textContent;

htmlContent = document.getElementsByClassName('wysihtml5-sandbox')[0].contentWindow.document.body.innerHTML;    

And my advice is that you shouldn't use jQuery for reaching the object when you listen events like keydown, keyup and keypress. It doesn't work properly.

The below codes work perfect for me;

$('.wysihtml5-sandbox').contents().find('body').on("keyup",function() {

    var content = document.getElementsByClassName('wysihtml5-sandbox')[0].contentWindow.document.body.textContent;
    var contentLength = content.length;
    var remaining = 2500-contentLength; 

    $("#characterCount").text(remaining);

});


来源:https://stackoverflow.com/questions/19152959/how-to-handle-wysihtml5s-br-and-nbsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!