当前位置: 代码迷 >> Web前端 >> 依照width截取字符串
  详细解决方案

依照width截取字符串

热度:89   发布时间:2012-08-31 12:55:03.0
按照width截取字符串
]/**
	 * 按照width截取字符串
	 */
	function cutstr(source, width) {
		var el = document.createElement("span");
		el.style.display = "none";
		document.body.appendChild(el);

		var span = $(el), s = "";

		for (var i = 0, len = source.length; i < len; i++) {
			span.text(span.text() + source.charAt(i));
			if (span.width() > width) {
				s = span.text();
				break;
			}
		}
		span.remove();
		span = null;
		return s ? s + "..." : source;
	};
  相关解决方案