JSFiddle - React, Tailwind, and code Playground

by dom1n1k

HTML

<div id="main">
    Lorem ipsum   dolor sit amet
    <br>
    <em> Italic</em>
    Tail text
</div>

JavaScript

var $contents = $("#main").contents();
/*
$contents.each(function () {
    console.log($(this));
    console.log(this.nodeType == 3);    
});
*/

var getTopLevelText = function (el) {
	return (
		$(el)
            .eq(0)
			.contents()
			.filter(function () {
                console.log('###' + this + '  ' + this.nodeType)
				return (this.nodeType == 3);
			}
        )
    );               
};		

var x = getTopLevelText( "#main" );
//console.log(x);
//console.log($.trim( x.text() ) );

 
// пойдем просто тупо в лоб
var h = $("#main").html();
console.log(h);
h = h.replace(/<.+?>/g, ' ');
h = h.replace(/[\s\r\n]{2,}/g, ' ');
h = $.trim(h);
console.log(h);