我正在尝试编写一个脚本来搜索整个页面,并从disabled
类型的所有对象中删除属性button
。完成此任务的最佳方法是什么?寻找简单的JS解决方案。
例如,我想转此:
<button disabled class="foo">My button</button>
成:
<button class="foo">My button</button>
该页面加载后将执行此脚本。
document.querySelectorAll('button').forEach(b=>b.removeAttribute('disabled'));
应该管用。
你可以在没有任何库的vanilla JavaScript中做到这一点。使用getElementsByTagName()
获取给定标记的所有元素,然后遍历它们并使用removeAttribute()
删除给定的属性。这是一个演示:
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>
如果需要为各个按钮使用值/属性,则可以使用此选项
$('button').each(function(){
$(this).prop('disabled', false);
});
要么
你可以简单地使用它
$('button').prop('disabled', false);
如果你使用的是JQuery,你可以做$('button[disabled]').removeAttr('disabled')