我必须加载 HTML 页面的正文,没有任何样式属性,没有链接图像以及所有非“纯文本”的内容。我想用 PHP 来做,并尝试了非常好的解决方案,但我还没有解决。我通过对脚本的 ajax 调用加载 html 页面,然后使用正则表达式获取正文,然后清除该正文。你能帮助我吗?这是ajax调用:
$.ajax({
type: "GET"
url: "core/proxy.php?url="+cerca,
success: function(data){
var body = data.replace(/^[\S\s]*<body[^>]*?>/i, "")
.replace(/<\/body[\S\s]*$/i, "");
$("div#risultato").html(body);
},
error: function(){
alert("failed");
}
});
});
您可以使用 jQuery 来获取
body
的文本内容。
因此,在您的
success
函数中,您将获取 data
,将其转换为 jQuery 对象并将文本插入到您的 div 中。
$('div#risultato').html($(data).find('body').text());
插入
style
后,您可以逐个标签地清除body
属性:
function clearStyles(element) {
element.setAttribute('style', '');
for (var i = 0; i < element.children.length; i++) {
clearStyles(element.children[i]);
}
}
clearStyles(document.body);
或者直接使用 jQuery:
jQuery('body *').attr('style', '');
Jose Antonio Riaza Valverde 我已更正,但没有任何变化:
$.ajax({
//definisco il tipo della chiamata
type: "GET",
//url della risorsa da contattare
url: "core/proxy.php?url="+cerca,
//azione in caso di successo
success: function(data)
{
var body = data.replace(/^[\S\s]*<body[^>]*?>/i, "")
.replace(/<\/body[\S\s]*$/i, "");
$("div#risultato").html(body);
clearStyles(document.getElementById('risultato'));
},
//azione in caso di errore
error: function()
{
alert("Chiamata fallita");
}
});
});
和功能:
function clearStyles(element) {
element.setAttribute('style', ' ');
element.setAttribute('img', ' ');
element.setAttribute('a', ' ');
for (var i = 0; i < element.children.length; i++) {
clearStyles(element.children[i]);
}
}