Javascript 未捕获类型错误:无法调用方法';hasAttribute';未定义的

Javascript 未捕获类型错误:无法调用方法';hasAttribute';未定义的,javascript,Javascript,我正在尝试检查按钮是否具有name属性。下面一行正确给出了结果 alert(course_view_buttons[30].hasAttribute("name")); 但是下面的代码给出了这样一个错误- "Uncaught TypeError: Cannot call method 'hasAttribute' of undefined " 完整的代码在这里- var course_view_buttons = document.getElementsByTagName("button")

我正在尝试检查按钮是否具有name属性。下面一行正确给出了结果

alert(course_view_buttons[30].hasAttribute("name"));
但是下面的代码给出了这样一个错误-

"Uncaught TypeError: Cannot call method 'hasAttribute' of undefined "
完整的代码在这里-

var course_view_buttons = document.getElementsByTagName("button");
alert(course_view_buttons.length);

var a=0;
for (x=0;x<=course_view_buttons.length;x++){

    if(course_view_buttons[x].hasAttribute("name");){
    a++;
}
}
alert(a);
var course\u view\u buttons=document.getElementsByTagName(“按钮”);
警报(路线、视图、按钮、长度);
var a=0;

对于(x=0;xfor循环条件,将其更改为

for (x=0;x < course_view_buttons.length;x++){

           ^_________ There is no <= which will give undefined as you are out of array bounds.
for(x=0;x^_________没有for循环条件,请将其更改为

for (x=0;x < course_view_buttons.length;x++){

           ^_________ There is no <= which will give undefined as you are out of array bounds.
for(x=0;x^_________没有问题是测试此表达式中的
是否小于或等于

x <= course_view_buttons.length

问题在于测试此表达式中的
是否小于或等于

x <= course_view_buttons.length