2011-04-04 54 views
0

上我目前有此:我如何通過每一個HTML元素遍歷網頁

$(document).ready(function() { 


$(this).each(function(index) { 

    delete style; 

    var style = $(this).attr("style"); 

    document.write(style); 

)); 

}); 

但是這不工作:/只是不斷告訴我style是不確定的。不太確定該從哪裏出發。

謝謝。

+0

我甚至不知道上面會在所有的工作,但要確保你和聲明或定義變量,您嘗試使用它們之前。 IE:在'var style = ...'之後移動'delete style'只是你知道,delete是用於數組和對象的。 – Shaz 2011-04-04 20:36:28

回答

0
$('*').each(function() { 
    var style = $(this).attr("style"); 
    document.write(style); // better replace to console.debug(style); with Firebug installed 
}) 

編輯:您當然不需要delete style語句,這是錯誤源。

+0

在什麼瀏覽器上對未定義的變量「刪除」會導致錯誤?我不認爲這是OP想要的,因爲載入後的'document.write'清除了現有的頁面。 – 2011-04-04 20:41:03

+1

對,代碼看起來像是爲實驗寫的,在這種情況下,我會使用'console.debug'而不是'document.write'。瀏覽器是IE – zindel 2011-04-04 20:50:10

0
$('*').each(function() { 
    document.write(this.style); 
    //or console.log(this.style) 
}); 

FYI this.style指元素的樣式對象。 $(本).attr(「風格」)將拉動元素的內嵌樣式

0

它會告訴你,因爲當你做delete style;,變量風格尚未定義的樣式是不確定的。

可以使用$('*')

迭代通過你的元素,你已經做的方式選擇的一切,它的罰款。你究竟想用刪除來做什麼?

$('*').each(function() { 
    var style = $(this).attr("style"); 
    document.write(style); 
}); 
0
$(document).find('*').each(function() 
{ 
$(this).removeAttr('style'); 
});