Xóa kiểu nội tuyến - Thủ thuật CSS

Anonim

Chức năng này cũng bảo tồn nội dung ẩn.

function remove_style(all) ( var i = all.length; var j, is_hidden; // Presentational attributes. var attr = ( 'align', 'background', 'bgcolor', 'border', 'cellpadding', 'cellspacing', 'color', 'face', 'height', 'hspace', 'marginheight', 'marginwidth', 'noshade', 'nowrap', 'valign', 'vspace', 'width', 'vlink', 'alink', 'text', 'link', 'frame', 'frameborder', 'clear', 'scrolling', 'style' ); var attr_len = attr.length; while (i--) ( is_hidden = (all(i).style.display === 'none'); j = attr_len; while (j--) ( all(i).removeAttribute(attr(j)); ) // Re-hide display:none elements, // so they can be toggled via JS. if (is_hidden) ( all(i).style.display = 'none'; is_hidden = false; ) ) )

Sử dụng

Gọi hàm như sau:

var all = document.getElementsByTagName('*'); remove_style(all);

Lưu ý: Việc chọn tất cả các phần tử trong trang qua truy vấn ký tự đại diện có thể chậm, tùy thuộc vào số lượng phần tử có trong trang. Bạn có thể sử dụng một tập hợp các phần tử nhỏ hơn để hoạt động hiệu quả hơn:

var set = document.getElementById('foo').getElementsByTagName('bar'); remove_style(set);

Mã của Nathan Smith.