[英]Find and remove attribute on all elements of a certain type
I'm trying to write a script that will search an entire page and remove the attribute disabled
from all objects of type button
. 我正在尝试编写一个脚本,该脚本将搜索整个页面并从button
类型的所有对象中删除disabled
的属性。 What is the best method to accomplish this? 什么是实现此目的的最佳方法? Looking for plain JS solutions. 寻找普通的JS解决方案。
For example I would like to turn this: 例如,我想转这个:
<button disabled class="foo">My button</button>
into: 变成:
<button class="foo">My button</button>
This script will be executed after the page loads. 该脚本将在页面加载后执行。
document.querySelectorAll('button').forEach(b=>b.removeAttribute('disabled'));
应该管用。
You can do that in vanilla JavaScript without any library. 您可以在没有任何库的原始JavaScript中进行操作。 Use getElementsByTagName()
to get all elements of a given tag, and then iterate through them and use removeAttribute()
to remove a given attribute. 使用getElementsByTagName()
获取给定标签的所有元素,然后遍历它们并使用removeAttribute()
删除给定属性。 Here is a demo: 这是一个演示:
var b = document.getElementsByTagName("button"); for (var i = 0; i < b.length; i++) { b[i].removeAttribute("disabled"); }
<button disabled class="foo">My 1st button</button> <button disabled class="foo">My 2nd button</button> <button disabled class="foo">My 3rd button</button>
You can use this if you need to use values/attributes for individual buttons 如果需要为单个按钮使用值/属性,则可以使用此选项
$('button').each(function(){
$(this).prop('disabled', false);
});
OR 要么
You can simply use this 你可以简单地使用这个
$('button').prop('disabled', false);
如果您使用的是JQuery,则可以执行$('button[disabled]').removeAttr('disabled')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.