我无法控制字符串来源,我只能<div class="page"></div>
,但我需要清理源代码中的混乱,
我想删除所有<div class="
但保留其内容,因为内容还包含可能无法完全完成的html标记,如<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>
http://jsbin.com/vukogobuze/1/edit?html,js,console,output
预期产出
/requests
答案 0 :(得分:1)
迭代.pageWrap
,生成一个包含HTML内容的临时元素作为文本内容,最后获取.page
中的html内容并加入它们。
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">e l\'É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>
更新:如果您要将内容更新到.source-wrapper
div,请使用html()
方法进行回调。
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">e l\'É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>
答案 1 :(得分:0)
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
}
}