【发布时间】:2008-12-29 12:59:29
【问题描述】:
我有一段文字,我希望显示被截断,但点击后它会展开以显示其余部分。再次单击应该会截断它。
我试图使用 onclick 事件按如下方式处理此问题(警告:请勿在未阅读以下内容的情况下运行以下代码...):
<span id='blah' onclick='showAllComment("this is a long comment to see it all", 9, true )'>this is a...</span>
<script>
function showAllComment( comment, shortCommentLen, showFullComment )
{
alert( $("#blah").html() );
if( showFullComment )
{
$("#blah").html( comment );
$("#blah").click( showAllComment( comment, shortCommentLen, false ) );
}
else
{
$("#blah").html( comment.substring( 0, shortCommentLen ) + "..." );
$("#blah").click( showAllComment( comment, shortCommentLen, true ) );
}
}
</script>
但正如您将看到的,它反复调用自己,您必须结束浏览器任务(因此运行此代码时要小心!!!!)
谁能建议为什么会发生这种情况,以及如何解决它。
提前致谢
【问题讨论】:
-
简短的
-
好点,但我不认为截断的评论实际上是“缩写”,它会滥用我认为的标签的最纯粹定义,其他人怎么看,这是最纯粹的吗? ?
标签: javascript jquery html