I have no control over the string source, I can only do html()
, but I need to clean up the mess in the source,
I want to delete all the <div class="page"></div>
but keep its content, and because the content also contains html tags which may not be fully completed like <div class="
http://jsbin.com/vukogobuze/1/edit?html,js,console,output
expected output
<div class="nodejs-class">La France està déployer ses porte-avions Charles de Gaulle pour soutenir les opérations contre l'État islamique ( IS) en Syrie et en Irak la présidence française a déclaré après une réunion du cabinet de défense</div>
Iterate over the .pageWrap
, generate a temporary element with HTML content as the text content and finally get the html content within .page
and join them.
var str = '<div id="source-wrapper"><div class="pageWrap"><div class="page">&lt;div class="nodejs-c</div></div><div class="pageWrap"><div class="page">lass"&gt;La France est </div></div><div class="pageWrap"><div class="page">à déployer ses porte</div></div><div class="pageWrap"><div class="page">-avions Charles de G</div></div><div class="pageWrap"><div class="page">aulle pour soutenir </div></div><div class="pageWrap"><div class="page">les opérations contr</div></div><div class="pageWrap"><div class="page">el\\'État islamique (</div></div><div class="pageWrap"><div class="page"> IS) en Syrie et en </div></div><div class="pageWrap"><div class="page">Irak la présidence f</div></div><div class="pageWrap"><div class="page">rançaise a déclaré a</div></div><div class="pageWrap"><div class="page">près une réunion du </div></div><div class="pageWrap"><div class="page">cabinet de défense&lt;/</div></div><div class="pageWrap"><div class="page">div&gt;</div></div></div>'; $('body').html( $(str) // get all pageWrap elements .find('.pageWrap') // iterate over the elements .map(function() { // generate a temporaray element with the // text content of current element as its html return $('<div>', { html: $(this).text() }) // get page element from the temporary element .find('.page') // get html content from page .html(); // instead of creating a temporary element you // can also do something simple like this by // parsing the content // return $.parseHTML($(this).text())[0].innerHTML }) // get result as an array .get() // join them .join('') );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
UPDATE : If you want to update content into the .source-wrapper
div then use html()
method with callback.
var str = '<div id="source-wrapper"><div class="pageWrap"><div class="page">&lt;div class="nodejs-c</div></div><div class="pageWrap"><div class="page">lass"&gt;La France est </div></div><div class="pageWrap"><div class="page">à déployer ses porte</div></div><div class="pageWrap"><div class="page">-avions Charles de G</div></div><div class="pageWrap"><div class="page">aulle pour soutenir </div></div><div class="pageWrap"><div class="page">les opérations contr</div></div><div class="pageWrap"><div class="page">el\\'État islamique (</div></div><div class="pageWrap"><div class="page"> IS) en Syrie et en </div></div><div class="pageWrap"><div class="page">Irak la présidence f</div></div><div class="pageWrap"><div class="page">rançaise a déclaré a</div></div><div class="pageWrap"><div class="page">près une réunion du </div></div><div class="pageWrap"><div class="page">cabinet de défense&lt;/</div></div><div class="pageWrap"><div class="page">div&gt;</div></div></div>'; $('body').html( $(str) // get all pageWrap elements .find('.pageWrap') // iterate over the elements .html(function() { // generate a temporaray element with the // text content of current element as its html return $('<div>', { html: $(this).text() }) // get page element from the temporary element .find('.page') // get html content from page .html(); // instead of creating a temporary element you // can also do something simple like this by // parsing the content // return $.parseHTML($(this).text())[0].innerHTML }).closest('#source-wrapper')[0].outerHTML );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
var list = document.getElementsByTagName("div");
for(i=0;i<list.length;i++){
var div = list[i];
if(div.class == "page"){
div.outerHTML = (div.outerHTML).replace('<div class="page">',"");
// Just do this again with '</div>' if you are sure there isnt a div inside of this one, else use this hard way
var diva = (div.outerHTML).split("</div>");
diva.pop(); // Remove the last </div> of the element
div.outerHTML = diva.join("</div>"); // Give back the removed DIV endings that arent the one you are replacing
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.