使用jQuery编程挑战

jon*_*bbs 3 javascript jquery

这是对前一个问题的跟进,似乎让人感到困惑,所以我会稍微净化一下.这是一些标记.

<div class="button width120 height30 iconLeft colorRed"></div>
<div class="button width180 height20 iconPlus colorBlue"></div>
<div class="button width200 height10 iconStar colorGreen"></div>
<div class="button width110 height60 iconBack colorOrange"></div>
Run Code Online (Sandbox Code Playgroud)

挑战是填写以下代码.

$(".button").each(function(){

    // Get the width from the class

    // Get the height from the class

    // Get the icon from the class

    // Get the color from the class

});
Run Code Online (Sandbox Code Playgroud)

现在,我知道你不应该以这种方式使用类,所以我不是在寻找替代方法,这是一个实验,我很想知道是否可以这样做.

ror*_*ryf 7

就像是:

$(".button").each(function(){
    var classNames = $(this).attr('class').split(' ');
    var width, height;
    for(int i = 0; i < classNames.length; i++) {
        var className = classNames[i];
        if(className.indexOf('width') > -1) {
            width = className.substring(5, className.length - 1);
        } else if(className.indexOf('height') > -1) {
            height = className.substring(6, className.length - 1);
        } // etc. etc.
    }
});
Run Code Online (Sandbox Code Playgroud)

或者我误解了你的要求?