簡體   English   中英

從PHP中的HTML刪除所有樣式屬性

[英]Remove all style attributes from HTML in PHP

我必須加載沒有任何樣式屬性,沒有鏈接圖像以及所有非純文本的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());

您可以在插入body后清除style屬性,逐個標記:

function clearStyles(element) {
    element.setAttribute('style', '');
    for (var i = 0; i < element.children.length; i++) {
        clearStyles(element.children[i]);
    }
}

clearStyles(document.body);

http://jsfiddle.net/n9ocxa0g/

或直接使用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]);
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM