直接看例子代码吧,下面这个是用来对一段较长的文字做查看全部和隐藏功能的代码
if(jQuery(".product-view .box-description .std").length){
//alert(jQuery(".product-view .box-description .std").text().substr(0,5));
var std=jQuery(".product-view .box-description .std");
var stdNormalLength=123;
var stdStr=std.text().toString();
var subStr=stdStr.subStr(0,stdNormalLength)+'......';
std.text(subStr);
var more=document.createElement("div");
more.innerHTML='<span class="more-std">显示更多</span>';
std.parent().append(more);
jQuery(".product-view .box-description .more-std").die().live("click",function(){
std.text(stdStr);
jQuery(this).text("隐藏");
jQuery(this).attr("class","less-std");
});
jQuery(".product-view .box-description .less-std").die().live("click",function(){
std.text(subStr);
jQuery(this).text("显示更多");
jQuery(this).attr("class","more-std");
});
}