计算文本宽度我正在尝试使用jQuery计算文本宽度。我不知道是什么,但我肯定做错了什么。下面是代码:var c = $('.calltoaction');var cTxt = c.text();var cWidth = cTxt.outerWidth();c.css('width' , cWidth);
3 回答
有只小跳蛙
TA贡献1824条经验 获得超8个赞
$.fn.textWidth = function(){
var html_org = $(this).html();
var html_calc = '<span>' + html_org + '</span>';
$(this).html(html_calc);
var width = $(this).find('span:first').width();
$(this).html(html_org);
return width;};
暮色呼如
TA贡献1853条经验 获得超9个赞
$.fn.textWidth = function(){
var self = $(this),
children = self.children(),
calculator = $('<span style="display: inline-block;" />'),
width;
children.wrap(calculator);
width = children.parent().width(); // parent = the calculator wrapper
children.unwrap();
return width;};.html
- 3 回答
- 0 关注
- 422 浏览
添加回答
举报
0/150
提交
取消
