在Javascript中使用类名禁用文本框
我有HTML在Javascript中使用类名禁用文本框,javascript,getelementsbyclassname,Javascript,Getelementsbyclassname,我有HTML Name 1:<input type="text" class="one" id="mytext"> <button onclick="disfunction()"></button> 但文本框仍处于启用状态。如何使用JAVASCRIPT中的类名禁用文本框 使用id我可以做到这一点。也可以使用jquery 但是我需要一个使用Javascript和classname返回元素列表的解决方案,您需要在其中循环以禁用每个元素: var cells =
Name 1:<input type="text" class="one" id="mytext">
<button onclick="disfunction()"></button>
但文本框仍处于启用状态。如何使用JAVASCRIPT中的类名禁用文本框
使用id我可以做到这一点。也可以使用jquery
但是我需要一个使用Javascript
和classname
返回元素列表的解决方案,您需要在其中循环以禁用每个元素:
var cells = table.getElementsByClassName("one");
for (var i = 0; i < cells.length; i++) {
cells[i].disabled = true;
}
var cells=table.getElementsByClassName(“一”);
对于(var i=0;i
JSFIDDLE:你可以试试这个,我敢打赌这就是你所看到的
function disfunction(){
document.getElementsByClassName("one")[0].disabled = true;
}
jsiddle:-Mozilla声明:
元素是已找到元素的实时HTMLCollection
因此,您必须遍历getElementsByClassName的结果
var testElements = document.getElementsByClassName('class-name-here');
var testDivs = Array.prototype.filter.call(testElements, function(testElement){
testElement.disabled = true;
});
为什么所有人都使用类而不是id?
var testElements = document.getElementsByClassName('class-name-here');
var testDivs = Array.prototype.filter.call(testElements, function(testElement){
testElement.disabled = true;
});