Hướng dẫn remove all attributes javascript - loại bỏ tất cả các thuộc tính javascript

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];
    }
};
1
const 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
const 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];
 }];
}];
3

HTML

$["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
const removeAttributes = [element] => {
    while [element.attributes.length > 0] {
        element.removeAttribute[element.attributes[0].name];
    }
};
6
const removeAttributes = [element] => {
    for [let i = 0; i  {
    while [element.attributes.length > 0] {
        element.removeAttribute[element.attributes[0].name];
    }
};
1
const element = document.getElementById[`example`];

removeAttributes[element];
3

const element = document.getElementById[`example`];

removeAttributes[element];
4
const element = document.getElementById[`example`];

removeAttributes[element];
5

const element = document.getElementById[`example`];

removeAttributes[element];
4
const element = document.getElementById[`example`];

removeAttributes[element];
7

const removeAttributes = [element] => {
    while [element.attributes.length > 0] {
        element.removeAttribute[element.attributes[0].name];
    }
};
1
const 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];
 }];
}];
7
const removeAttributes = [element] => {
    while [element.attributes.length > 0] {
        element.removeAttribute[element.attributes[0].name];
    }
};
6
const 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];
 }];
}];
1
const 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];
4while1

while2while3

while4while5

while6while7

while8while9

while6removeAttributes1

while4removeAttributes1

while2removeAttributes5

while2removeAttributes7

const element = document.getElementById[`example`];

removeAttributes[element];
4removeAttributes1

const removeAttributes = [element] => {
    while [element.attributes.length > 0] {
        element.removeAttribute[element.attributes[0].name];
    }
};
1removeAttributes1

$["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
const removeAttributes = [element] => {
    while [element.attributes.length > 0] {
        element.removeAttribute[element.attributes[0].name];
    }
};
6
const 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];
 }];
}];
1example0
$["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];
 }];
}];
1example4
$["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

const removeAttributes = [element] => {
    while [element.attributes.length > 0] {
        element.removeAttribute[element.attributes[0].name];
    }
};
1
const removeAttributes = [element] => {
    while [element.attributes.length > 0] {
        element.removeAttribute[element.attributes[0].name];
    }
};
2

const 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];
 }];
}];
04

const removeAttributes = [element] => {
    while [element.attributes.length > 0] {
        element.removeAttribute[element.attributes[0].name];
    }
};
1
const removeAttributes = [element] => {
    while [element.attributes.length > 0] {
        element.removeAttribute[element.attributes[0].name];
    }
};
6example8
$["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

const 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];
 }];
}];
3

const element = document.getElementById[`example`];

removeAttributes[element];
4
const removeAttributes = [element] => {
    while [element.attributes.length > 0] {
        element.removeAttribute[element.attributes[0].name];
    }
};
2

const element = document.getElementById[`example`];

removeAttributes[element];
4
const 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];
 }];
}];
1
const 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];
 }];
}];
1example8
const element = document.getElementById[`example`];

removeAttributes[element];
0
const 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];
 }];
}];
3

const 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];
 }];
}];
48
const 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
const 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];
 }];
}];
7
const removeAttributes = [element] => {
    while [element.attributes.length > 0] {
        element.removeAttribute[element.attributes[0].name];
    }
};
6example4
$["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

const removeAttributes = [element] => {
    while [element.attributes.length > 0] {
        element.removeAttribute[element.attributes[0].name];
    }
};
6example0
$["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

const 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];
 }];
}];
3

Output:


Bài Viết Liên Quan

Chủ Đề