How can I iterate through every single HTML element on a web page
I currently have this:
$(document).ready(function() {
$(this).each(function(index) {
delete style;
var style = $(this).attr("style");
document.write(style);开发者_StackOverflow
));
});
However this is not working :/ Just keeps telling me style
is undefined. Not quite sure where to go from there.
Thanks.
$('*').each(function() {
document.write(this.style);
//or console.log(this.style)
});
FYI this.style refers to the element's style object. $(this).attr('style') will pull the element's INLINE style
It tells you that style is undefined because when you do delete style;
, the variable style has not been defined.
You can select everything using $('*')
Iterate through your elements the way you already do it, it's fine. What exactly are you trying to do with the delete though?
$('*').each(function() {
var style = $(this).attr("style");
document.write(style);
});
$(document).find('*').each(function()
{
$(this).removeAttr('style');
});
$('*').each(function() {
var style = $(this).attr("style");
document.write(style); // better replace to console.debug(style); with Firebug installed
})
Edit: You certainly don't need the delete style
statement, that's the error source.
精彩评论