我想将指定的标签文本设置为跨度宽度。这是代码。谢谢你的帮助。
function setBarWidth(labelText, barElement, cssProperty, barPercent) {
var listData = [];
$(labelText).each(function () {
listData.push($(this).textContent);
});
var listMax = Math.max.apply(Math, listData);
$(barElement).each(function(index) {
$(this).css(cssProperty, (listData[index] / listMax) * barPercent + "%");
});
}
setBarWidth(".style-1 span label", ".style-1 em", "width", 100);
setBarWidth(".style-2 span label", ".style-2 span", "width", 55);发布于 2016-02-24 05:41:26
textContent是DOM属性,但$(this)是jQuery对象。您应该使用this.textContent或$(this).text()。
您还可以用.map替换.each
var listData = $(labelText).map(function() {
return this.textContent;
}).get();最后的.get()是将jQuery集合转换为普通数组所必需的。
https://stackoverflow.com/questions/35588687
复制相似问题