【发布时间】:2010-08-31 08:35:54
【问题描述】:
我正在尝试实现一个要求,该要求要求我为 div 元素的内容设置最大行长。但是当我在 Firefox 中打开页面时,我收到关于“长时间运行的脚本”的错误,并被要求继续、调试或停止脚本。我不明白为什么会这样。这是我的代码:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$.fn.lineCount = function () {
var lineHeight = this.css("line-height"); // returns "Xpx"
return document.getElementById(this.attr("id")).offsetHeight / parseInt(lineHeight.substr(0, lineHeight.length - 2));
};
$.fn.truncateLineCount = function (countAfterTruncation) {
while (this.lineCount() > countAfterTruncation) {
var text = this.html();
this.html(text.substr(0, text.length - 1) + "…");
}
};
$(function () {
$("#title").truncateLineCount(3);
});
</script>
</head>
<body>
<div id="title">
My MVC Application<br />
steaks<br />
are<br />
awesome
</div>
</body>
</html>
什么给了?
【问题讨论】:
-
我认为您的
truncateLineCount函数不会改变行数。 -
不是手头的问题,但值得一提:parseInt() 接受它的字符串参数并在开始时查找一个整数,没有必要对“px”进行切片“结束。例如,
parseInt("12px")将返回12。
标签: javascript jquery string truncate