How to handle wysihtml5`s br and nbsp?

2019-08-11 05: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);

});