javascript - 使用 jQuery 的编程挑战

标签 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>

挑战是填写下面的代码。

$(".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

});

现在,我知道您不应该以这种方式使用类,所以我没有寻找其他替代方式,这是一个实验,我很想知道是否可以采用这种方式。

最佳答案

类似于:

$(".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.
    }
});

还是我误解了你的意思?

关于javascript - 使用 jQuery 的编程挑战,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/756443/

相关文章:

jquery - 如何添加 jQuery 循环来汇总每个输入

javascript - 当访问者在页面上的特定类别时使文本颜色不同

javascript - Angular JS UI-Router 示例不起作用

dom - 将 javascript 函数注入(inject) Iframe

javascript - Google Analytics 是否捕获 Javascript 重定向?

javascript - 动画结束时执行动画

Javascript 映射函数将值转换为十进制

javascript - JavaScript 中的 Microsoft 认知服务示例

javascript测试值是否在数组中

javascript - 在 Chart.js 中设置图表高度