这是对上一个问题的跟进,这个问题似乎让人们感到困惑,所以我会稍微提炼一下。这是一些标记。
<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/