Sanitizer: removeAttribute() 方法
Sanitizer
接口的 removeAttribute()
方法将一个属性设置为不允许在所有元素上使用。
指定的属性会被添加到此 sanitier 配置的 removeAttributes
列表中。如果该属性存在于 attributes
列表中,则会从中移除。
请注意,如果只想允许/不允许特定元素上的属性,请使用 Sanitizer.allowElement()
。
语法
js
removeAttribute(attribute)
参数
attribute
-
一个字符串,指示要全局禁止在元素上使用的属性的名称,或者一个具有以下属性的对象
name
-
包含属性名称的字符串。
namespace
可选-
一个包含属性命名空间的字符串,默认为
null
。
返回值
无 (undefined
)。
示例
如何禁止特定属性
此示例显示了如何使用 removeAttribute()
来指定应从元素中移除某个属性。
JavaScript
代码首先创建一个新的 Sanitizer
对象,该对象最初不指定任何属性或元素。然后,我们调用 removeAttribute()
并传入属性 title
和 mathcolor
。
js
// Create sanitizer that allows
const sanitizer = new Sanitizer({
removeAttributes: [],
});
// Remove the title attribute
sanitizer.removeAttribute("title");
// Remove the mathcolor attribute
sanitizer.removeAttribute("mathcolor");
// Log the sanitizer configuration
let sanitizerConfig = sanitizer.get();
log(JSON.stringify(sanitizerConfig, null, 2));
结果
最终配置将记录在下方。请注意,这两个属性都已添加到 removeAttributes
列表中(当使用 sanitier 时,如果这些属性存在于元素上,它们将被移除)。
规范
规范 |
---|
HTML Sanitizer API # dom-sanitizer-removeattribute |
浏览器兼容性
加载中…