如何限制html标签属性验证?
How to restrict html tag attribute validation?
您好,已创建如下 html 元素:
var element = document.createElement('div');
element.innerHTML = `<img src="fail" onerror="alert();" />`;
element.setAttribute('id', 'check');
document.body.append(element);
// removed element from DOM
element.parentNode.removeChild(element);
当您 运行 此代码时,您将看到如下控制台错误:
GET https://stacksnippets.net/fail 404 (Not Found)
是否有任何解决此控制台错误的建议?
我尝试了下面的 lke 并解决了我的 pblm
private static removeElement(): void {
// Removes an element's attibute to avoid html tag validation
let nodes: HTMLCollection = this.wrapElement.children;
for (let j: number = 0; j < nodes.length; j++) {
let attribute: NamedNodeMap = nodes[j].attributes;
for (let i: number = 0; i < attribute.length; i++) {
this.wrapElement.children[j].removeAttribute(attribute[i].localName);
}
}
}
您好,已创建如下 html 元素:
var element = document.createElement('div');
element.innerHTML = `<img src="fail" onerror="alert();" />`;
element.setAttribute('id', 'check');
document.body.append(element);
// removed element from DOM
element.parentNode.removeChild(element);
当您 运行 此代码时,您将看到如下控制台错误:
GET https://stacksnippets.net/fail 404 (Not Found)
是否有任何解决此控制台错误的建议?
我尝试了下面的 lke 并解决了我的 pblm
private static removeElement(): void {
// Removes an element's attibute to avoid html tag validation
let nodes: HTMLCollection = this.wrapElement.children;
for (let j: number = 0; j < nodes.length; j++) {
let attribute: NamedNodeMap = nodes[j].attributes;
for (let i: number = 0; i < attribute.length; i++) {
this.wrapElement.children[j].removeAttribute(attribute[i].localName);
}
}
}