Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当id';不同的是_Javascript_Jquery_Html - Fatal编程技术网

Javascript 当id';不同的是

Javascript 当id';不同的是,javascript,jquery,html,Javascript,Jquery,Html,我想在特定时间禁用某组按钮。使用此js: document.getElementById("reset").setAttribute('disabled','disabled'); 但所有ID都不同,按钮类似于等。我可以设置一些组合变量,如 然后像这样做: document.getElementById("group:hello").setAttribute('disabled','disabled'); 要禁用所有按钮,即使每个按钮的ID属性不同?使用类而不是ID。 代码很简单: docu

我想在特定时间禁用某组按钮。使用此js:

document.getElementById("reset").setAttribute('disabled','disabled');
但所有ID都不同,按钮类似于
等。我可以设置一些组合变量,如

然后像这样做:

document.getElementById("group:hello").setAttribute('disabled','disabled');
要禁用所有按钮,即使每个按钮的ID属性不同?

使用类而不是ID。 代码很简单:

document.getElementsByClassName("group1").setAttribute('disabled','disabled');

因为您标记了jquery

$(".mybutton").attr("disabled", true);
我的按钮在哪里

<button class= "mybutton"> </button>

使用
class
而不是
id
,如下所示:

var elements = document.getElementsByClassName("{yourClassName}");
var i;
for (i = 0; i < x.length; i++) {
    elements[i].setAttribute(“disabled”, “red");
}

<button class="{yourClassName}”></button>
var elements=document.getElementsByClassName(“{yourClassName}”);
var i;
对于(i=0;i您可以使用
getElementsByTagName
获取所有按钮,然后可以使用
Array.Prototype.forEach
禁用所有按钮,如下所示。
forEach
将循环遍历所有元素,并向所有找到的元素添加所需属性(在您的情况下为禁用)

Array.prototype.forEach.call
(document.getElementsByTagName(“按钮”),函数(e){
e、 setAttribute('disabled','disabled');
});
12

12 12您可以对Id使用通配符:

从选择器开始

document.querySelector("[id^=myId]")
document.querySelector("[id$=myId]")
为…工作

id="myId1"
id="myId2"
...
id="firstMyId"
id="secondMyId",...
或以选择器结束

document.querySelector("[id^=myId]")
document.querySelector("[id$=myId]")
为…工作

id="myId1"
id="myId2"
...
id="firstMyId"
id="secondMyId",...
提示:
请不要用诸如:Id=“1myId”之类的数字开始Id。这不是w3c conform

使用类对按钮进行分组,如下所示:

var btnsGroup = document.getElementsByClassName('group');
Array.prototype.forEach.call(btnsGroup, function(button) {
  button.setAttribute('disabled','disabled');
});

您可以使用数据属性,请参见下一步:

<button  data-group="hello" id="button1">test1</button>
<button  data-group="hello" id="button2">test2</button>
<button  data-group="hello" id="button3">test3</button>

编辑,这是小提琴

你可以这样做-

var buttons = document.querySelectorAll("[reset='true']");

for (i = 0; i < buttons.length; ++i) 
{
    buttons[i].setAttribute('disabled','disabled');
}
<button reset="true" >1 Candy</button>
<button reset="true" >2 Candy</button>
var buttons=document.querySelectorAll(“[reset='true']”);
对于(i=0;i
HTML是这样的-

var buttons = document.querySelectorAll("[reset='true']");

for (i = 0; i < buttons.length; ++i) 
{
    buttons[i].setAttribute('disabled','disabled');
}
<button reset="true" >1 Candy</button>
<button reset="true" >2 Candy</button>
1糖果
2糖果

您可以给出元素类名称,然后使用
getElementsByClassName()
来检索它们。这将返回一个元素列表,您可以遍历该列表并分别设置每个元素的属性。使用jQuery,这会更容易一些。这只会给您第一个元素不起作用。所有按钮中只有一个按钮禁用,即使它们具有相同的类,您可以将forEach链接到querySelectorAll,而不使用c正在创建buttonList变量。@Kirill我不确定,但我猜forEach在某些浏览器中存在兼容性问题。我可能是错的。@Zicsus IE9+与querySelectorAll相同:querySelectorAll与forEach具有相同的浏览器支持,因此您最好使用它而不是for循环。否则,使用旧的DOM导航功能以获得更好的浏览效果是值得的er支持。@Kirill是的,我这样做是因为浏览器支持。