Class^='instructions'...只有 select 以 class 开头的第一个元素应该 select 所有

Class^='instructions'...Only selects first element beginning with class should select all

这个代码块有什么问题?它只会 select 第一个包含指令的元素,但不会 select 所有指令。为什么?我错过了什么?

jQuery(document).ready(function () {

    jQuery('select').select2();

    // get a list of objects that begin with instructions
    var classList =  jQuery('a[class^="instructions"]').attr('class').split(/\s+/);
    console.log(classList);
    jQuery.each(classList, function (index, item) {
        jQuery('.'+item+'-'+index).click(
            function (event) {
                event.preventDefault();
                jQuery(this).parent().next().toggle();
            }
        );
    });
});
当用作 getter 时,

attr() 只能 return 来自一个元素的值,并且总是 return 来自集合中的第一个元素。

您需要为所有元素映射您自己的数组,例如:

var classList =  jQuery('a[class^="instructions"]').map(function(){
  return $(this).attr('class');
}).get();