Một lý do rất chính đáng để làm điều này cho các thẻ cụ thể là để làm sạch nội dung kế thừa và cũng thực thi các tiêu chuẩn.
Ví dụ, giả sử, bạn muốn loại bỏ các thuộc tính cũ hoặc hạn chế thiệt hại do các thuộc tính thẻ phông chữ gây ra bằng cách tước chúng.
Tôi đã thử một số phương pháp để đạt được điều này và không có, bao gồm cả ví dụ trên, hoạt động như mong muốn.
Ví dụ 1: Thay thế tất cả các thẻ phông chữ bằng nội dung văn bản chứa. Đây sẽ là giải pháp hoàn hảo nhưng kể từ v1.6.2 đã ngừng hoạt động. :[
$['#content font'].each[function[i] {
$[this].replaceWith[$[this].text[]];
}];
Ví dụ 2: Dải tất cả các thuộc tính từ thẻ có tên - ví dụ: NÉT CHỮ. Một lần nữa, điều này không hoạt động nhưng chắc chắn rằng nó đã từng hoạt động một lần vào phiên bản jQuery trước đó.
$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
Mong muốn 1.7 hứa hẹn sẽ bao gồm một phương thức để xóa nhiều thuộc tính theo tên.
Ngày 8 tháng 11 năm 2021 - 1 phút đọc Hàm JavaScript này sẽ chấp nhận một phần tử HTML đã cho và xóa tất cả các thuộc tính của nó:const removeAttributes = [element] => {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
Nếu bạn không muốn sử dụng vòng lặp while
, thì đây là hàm removeAttributes
thay thế:
const removeAttributes = [element] => {
for [let i = 0; i {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
1const removeAttributes = [element] => {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
4$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
7const removeAttributes = [element] => {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
6$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
9$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
3HTML
$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
7const removeAttributes = [element] => {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
6const removeAttributes = [element] => {
for [let i = 0; i {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
1const element = document.getElementById[`example`];
removeAttributes[element];
3const element = document.getElementById[`example`];
removeAttributes[element];
4const element = document.getElementById[`example`];
removeAttributes[element];
5const element = document.getElementById[`example`];
removeAttributes[element];
4const element = document.getElementById[`example`];
removeAttributes[element];
7const removeAttributes = [element] => {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
1const element = document.getElementById[`example`];
removeAttributes[element];
9$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
7const removeAttributes = [element] => {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
6const element = document.getElementById[`example`];
removeAttributes[element];
0$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
3$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
7$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
1const removeAttributes = [element] => {
for [let i = 0; i {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
1$.fn.removeAllAttributes = function[] { return this.each[function[] { $.each[this.attributes, function[] { this.ownerElement.removeAttributeNode[this]; }]; }]; }; $['textarea'].removeAllAttributes[];9
const element = document.getElementById[`example`];
removeAttributes[element];
4while
1while
2while
3
while
4while
5
while
6while
7
while
8while
9
while
6removeAttributes
1
while
4removeAttributes
1
while
2removeAttributes
5
while
2removeAttributes
7
const element = document.getElementById[`example`];
removeAttributes[element];
4removeAttributes
1const removeAttributes = [element] => {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
1removeAttributes
1$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
7const removeAttributes = [element] => {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
6const removeAttributes = [element] => {
for [let i = 0; i {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
6$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
5$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
3$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
1example
0$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
3$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
7$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
1example
4$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
3const removeAttributes = [element] => {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
1const removeAttributes = [element] => {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
2const element = document.getElementById[`example`];
removeAttributes[element];
4$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
04const removeAttributes = [element] => {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
1const removeAttributes = [element] => {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
6example
8$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
3const removeAttributes = [element] => {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
1$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
1$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
11$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
3const element = document.getElementById[`example`];
removeAttributes[element];
4const removeAttributes = [element] => {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
2const element = document.getElementById[`example`];
removeAttributes[element];
4const removeAttributes = [element] => {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
4$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
7$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
1const removeAttributes = [element] => {
for [let i = 0; i {
for [let i = 0; i {
for [let i = 0; i {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
1$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
1example
8 const element = document.getElementById[`example`];
removeAttributes[element];
0const removeAttributes = [element] => {
for [let i = 0; i {
for [let i = 0; i {
for [let i = 0; i {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
1$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
1$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
41$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
42$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
41$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
3const removeAttributes = [element] => {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
1$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
1$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
47 $["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
48const removeAttributes = [element] => {
for [let i = 0; i {
for [let i = 0; i {
for [let i = 0; i {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
1const removeAttributes = [element] => {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
6$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
47$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
3$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
7const removeAttributes = [element] => {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
6example
4$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
3const removeAttributes = [element] => {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
6example
0$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
3const removeAttributes = [element] => {
while [element.attributes.length > 0] {
element.removeAttribute[element.attributes[0].name];
}
};
6$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
2$["font"].each[function[] {
// First copy the attributes to remove.
var attributes = $.map[this.attributes, function[item] {
return item.name;
}];
// Now remove the attributes
var font = $[this];
$.each[attributes, function[i, item] {
$["font"].removeAttr[item];
}];
}];
3Output: