@liayun
2016-05-13T04:01:07.000000Z
字数 6820
阅读 2328
知问前端
本文需要从数据库里获取相应数据,然后转换为JSON模式,最终在页面上显示出来。我们希望显示的时候,隐藏大部分,显示摘要,具有切换功能。
从服务器端获取数据,转化为JSON格式。
show_content.php:
<?phprequire 'config.php';$query = mysql_query("SELECT title,content,user,date FROM question ORDER BY date DESC LIMIT 0,5") or die('SQL 错误!');$json = '';while (!!$row = mysql_fetch_array($query, MYSQL_ASSOC)) {//json格式转码foreach ( $row as $key => $value ) {$row[$key] = urlencode(str_replace("\n","", $value));$row[$key] = urlencode(str_replace("\t","", $value));}$json .= urldecode(json_encode($row)).',';//$json .= json_encode($row).','; //得到json格式//print_r($row); //以php数组的形式显示出来,javascript不能直接用,那么最好将其保存为json格式}//echo $json;echo '['.substr($json, 0, strlen($json) - 1).']'; //一个json数组,一个数组有3个对象,每个对象就是一个jsonmysql_close();?>
为了从数据库表question中取出的数据能在页面中有组织的显示出来,所以可以假设这样组织:
<div id="main"><div class="main_left"><div id="tabs"><ul><li><a href="tab1.html">tab1</a></li><li><a href="tab2.html">tab2</a></li><li><a href="tab3.html">tab3</a></li></ul></div><div class="content"><h4>li 发表于 2016-05-07</h4><h3>外貌和魅力方面被打击该如何调整心态?</h3><div class="editor">123</div><div class="bottom">0条评论 <span class="down">显示全部</span><span class="up">收起</span></div><hr noshade="noshade" size="1" /></div></div><div class="main_right"><div id="accordion"><h3>菜单1</h3><div><p>内容1</p><p>内容1</p></div><h3>菜单2</h3><div>内容2</div><h3>菜单3</h3><div>内容3</div></div></div></div>
我们重点关注其中的class="content"的div,即:
<div class="content"><h4>li 发表于 2016-05-07</h4><h3>外貌和魅力方面被打击该如何调整心态?</h3><div class="editor">123</div><div class="bottom">0条评论 <span class="down">显示全部</span><span class="up">收起</span></div><hr noshade="noshade" size="1" /></div>
接下来可以添加css样式:
.content h4 {color:#666;font-weight:normal;}.content h3 {color:#369;}.content .editor {line-height: 180%;height: 155px;overflow: hidden;color:#333;}.content .bottom {padding:5px 0 0 0;}.content hr {color:#ccc;height:1px;}.content .up {float:right;color:#369;cursor:pointer;}.content .down {float:right;color:#369;cursor:pointer;}
我们尤其应注意.content .editor选择符中的属性height,我们是以155px的高度作为临界线的。
> 155px,那么<span class="up">收起</span>隐藏,<div class="editor"></div>高度置为155px。<= 155px,<span class="down">显示全部</span>和<span class="up">收起</span>均隐藏,<div class="editor"></div>高度就为问题描述的原始高度。如图所示:

如果按照这个思路,那么
HTML部分代码:
<div id="main"><div class="main_left"><div id="tabs"><ul><li><a href="tab1.html">tab1</a></li><li><a href="tab2.html">tab2</a></li><li><a href="tab3.html">tab3</a></li></ul></div><div class="content"></div></div><div class="main_right"><div id="accordion"><h3>菜单1</h3><div><p>内容1</p><p>内容1</p></div><h3>菜单2</h3><div>内容2</div><h3>菜单3</h3><div>内容3</div></div></div></div>
CSS样式:
.content h4 {color:#666;font-weight:normal;}.content h3 {color:#369;}.content .editor {line-height: 180%;overflow: hidden;color:#333;}.content .bottom {padding:5px 0 0 0;}.content hr {color:#ccc;height:1px;}.content .up {float:right;color:#369;cursor:pointer;}.content .down {float:right;color:#369;cursor:pointer;}
jQuery代码如下:
$.ajax({url : "show_content.php",type : "post",success : function(response, status, xhr) {//alert(response); //取出数据//alert(typeof response); //string//alert(typeof $.parseJSON(response)); //object//alert($.parseJSON(response)); //[object Object],[object Object],[object Object]var json = $.parseJSON(response); //parseJSON方法有问题var html = "";var arr = [];//alert(json.length); //3$.each(json, function(index, value) {//alert(value.title);//alert(index);html += "<h4>" + value.user + " 发表于 " + value.date + "</h4><h3>"+value.title+"</h3><div class='editor'>" + value.content + "</div><div class='bottom'>0条评论 <span class='down'>显示全部</span><span class='up'>收起</span></div><hr noshade='noshade' size='1' />"});$(".content").append(html);$.each($(".editor"), function(index, value) {//arr[index] = value;//alert($(value).height());arr[index] = $(value).height(); //每个内容区块的原始高度if ($(value).height() > 155) {$(value).next(".bottom").find(".up").hide();$(value).height(155);} else {$(value).next(".bottom").find(".up").hide();$(value).next(".bottom").find(".down").hide();}});$.each($(".bottom .down"), function(index, value) {$(this).click(function() {$(this).parent().prev().height(arr[index]);$(this).hide();$(this).parent().find(".up").show();});});$.each($(".bottom .up"), function(index, value) {$(this).click(function() {$(this).parent().prev().height(155);$(this).hide();$(this).parent().find(".down").show();});});}});
注意:可能出现出现两个问题。
问题一:
$.parseJSON(response);可能报错,关于这个问题,可以参考SyntaxError: JSON.parse: bad control character in string literal...,在此并不赘述。由于第一种方法,可能会造成溢出的字体成半截形式。所以,我们将再使用另外一套思路,字符串截取的方式来实现这个功能。
HTML部分代码不变,仍为:
<div id="main"><div class="main_left"><div id="tabs"><ul><li><a href="tab1.html">tab1</a></li><li><a href="tab2.html">tab2</a></li><li><a href="tab3.html">tab3</a></li></ul></div><div class="content"></div></div><div class="main_right"><div id="accordion"><h3>菜单1</h3><div><p>内容1</p><p>内容1</p></div><h3>菜单2</h3><div>内容2</div><h3>菜单3</h3><div>内容3</div></div></div></div>
CSS样式变为:
.content h4 {color:#666;font-weight:normal;}.content h3 {color:#369;}.content .editor {line-height: 180%;color:#333;}.content .bottom {padding:5px 0 0 0;}.content hr {color:#ccc;height:1px;}.content .up {float:right;color:#369;cursor:pointer;}.content .down {font-weight: normal;color:#369;cursor:pointer;}
jQuery代码变为:
$(function() {$.ajax({url : "show_content.php",type : "post",success : function(response, status, xhr) {//alert(response); //取出数据//alert(typeof response); //string//alert(typeof $.parseJSON(response)); //object//alert($.parseJSON(response)); //[object Object],[object Object],[object Object]var json = $.parseJSON(response); //parseJSON方法有问题var html = "";var arr = [];var summary = [];//alert(json.length); //3$.each(json, function(index, value) {//alert(value.title);//alert(index);html += "<h4>" + value.user + " 发表于 " + value.date + "</h4><h3>"+value.title+"</h3><div class='editor'>" + value.content + "</div><div class='bottom'>0条评论 <span class='up'>收起</span></div><hr noshade='noshade' size='1' />"});$(".content").append(html);$.each($(".editor"), function(index, value) {arr[index] = $(value).html();summary[index] = arr[index].substr(0, 200);if (summary[index].substring(199, 200) == "<") {summary[index] = replacePos(summary[index], 200, "");}if (summary[index].substring(198, 200) == "</") {summary[index] = replacePos(summary[index], 200, "");summary[index] = replacePos(summary[index], 199, "");}if(arr[index].length > 200) {summary[index] += '...<span class="down">显示全部</span>'; //一个大的问题,显示全部为动态添加进去,隐藏掉再添加进去的时候,click事件会失效$(value).html(summary[index]);}$(".bottom .up").hide();});//动态绑定,用委托绑定处理$.each($(".editor"), function(index, value) {$(this).on("click", ".down", function() {$(".editor").eq(index).html(arr[index]);$(this).hide();$(".bottom .up").eq(index).show();});});//统一处理$.each($(".bottom"), function(index, value) {$(this).on("click", ".up", function() {$(".editor").eq(index).html(summary[index]);$(this).hide();$(".editor .down").eq(index).show();});});}});});//替换特殊字符的函数function replacePos(strObj, pos, replaceText) {return strObj.substr(0, pos-1) + replaceText + strObj.substring(pos, strObj.length);}
问题:显示全部为动态添加进去,隐藏掉再添加进去的时候,click事件会失效,所以需要使用用委托绑定处理。
显示效果:
