在前端应用中,经常需要在js中动态拼接HTML页面,比如应用ajax进行局部刷新的时候,就需要在js中拼接HTML页面。
主要规则是将HTML页面的标签拼接为标签字符创,要特别注意的是标签中的变量或数值。
小编在此只贴出部分关键代码
<table id="serial_tab"></table>
在上述的table标签中拼接页面。拼接页面的内容如下:
$(function(){
//首先获取串口数量 然后动态生成table表格
$.ajaxSettings.async = false; //同步
$.getJSON("scanner!totalSerialCount.action","",function(result){
var length = result["count"];
if(result != null){
if(result["count"] ==0){
$.dialog.tips("未检测到串口设备",1,"hits.png");
return ;
}else{
for(var i=1; i<= length;i++){
//拼接列表页面
var tableContent = "";
tableContent += '<tr>';
tableContent += '<td width="9%;" height="38">com'+i+' 波特率</td>'; //注意变量的拼接方式
tableContent += '<td width="200px;">';
tableContent += '<select name="baud" id="baud'+i+'" style="width:150px;height:28px;" class="df">';
tableContent += '<option value="110">110</option>';
tableContent += '<option value="300">300</option>';
tableContent += '<option value="600" >600</option>';
tableContent += '<option value="1200">1200</option>';
tableContent += '<option value="2400">2400</option>';
tableContent += '<option value="4800">4800</option>';
tableContent += '<option value="9600" >9600</option>';
tableContent += '<option value="14400">14400</option>';
tableContent += '<option value="19200">19200</option>';
tableContent += '<option value="38400">38400</option>';
tableContent += '<option value="57600">57600</option>';
tableContent += '<option value="115200">115200</option>';
tableContent += '<option value="230400">230400</option>';
tableContent += '<option value="460800">460800</option>';
tableContent += '<option value="1024000">1024000</option>';
tableContent += '</select>';
tableContent += '</td>';
tableContent += '<td width="5%;">数据位</td>';
tableContent += '<td width="200px;">';
tableContent += '<select name="databit" id="databit'+i+'" style="width:150px;" class="df">';
tableContent += '<option value="5">5</option>';
tableContent += '<option value="6">6</option>';
tableContent += '<option value="7">7</option>';
tableContent += '<option value="8" >8</option>';
tableContent += '</select>';
tableContent += '</td>';
tableContent += '<td width="5%;">停止位</td>';
tableContent += '<td width="200px;">';
tableContent += '<select name="stopbit" id="stopbit'+i+'" style="width:150px;" class="df">';
tableContent += '<option value="1" >1</option>';
tableContent += '<option value="2" >2</option>';
tableContent += '</select>';
tableContent += '</td>';
tableContent += '<td width="5%;">校验位</td>';
tableContent += '<td width="200px;">';
tableContent += '<select name="checkbit" id="checkbit'+i+'" style="width:150px;" class="df">';
tableContent += '<option value="None">无校验 None</option>';
tableContent += '<option value="e">偶校验 Even</option>';
tableContent += '<option value="o">奇校验 Odd</option>';
tableContent += '<option value="m">标记 Mark</option>';
tableContent += '<option value="s">空格 Space</option>';
tableContent += '</select>';
tableContent += '</td>';
tableContent += '<td>';
tableContent += '<input type="button" class="btn" value="确认保存" onclick="saveConfig('+i+')" style="margin-top:3px;"/>';
tableContent += '</td>';
tableContent += '</tr>';
//将拼接的内容追加到table标签里面
$("#serial_tab").append(tableContent);
}
}
}
});
loadConfig();
});
最后页面的结果如下图所示: 大家莫见怪,css 样式代码我就不贴出来了
注意事项:
1.在HTML页面中拼接时,需要特别注意含有事件的拼接以及拼接事件中的参数形式。
本人亲自经历:在刚开始拼接时,拼接的形式为:
<a href="javascript:void(0);" onclick="delComment('+commentInfo.commentId+','+activityId +');" class="btn btn-white m-r-5"><i class="fa fa-trash-o"></i></a>
拼装好之后,进行触发的时候,一直没有反应,原因是里面的参数的格式不能识别,需要将其变为字符串形式。正确的拼装形式为:
<a href="javascript:void(0);" onclick=delComment("'+commentInfo.commentId+'","'+activityId +'") class="btn btn-white m-r-5"><i class="fa fa-trash-o"></i></a>
拼装好之后,点击进行触发就可识别。
2.当拼装的HTML里面需要获取后台中的值时,也可以用jQuery的形式获取:${userId}。
不过在拼装的时候,尽量先将他获取出来,然后再拼装页面的时候,直接使用就好了,避免出错的几率。
3.标签中间的文本中变量的使用拼接方式:
tableContent += '<td ><span class="badge badge-info">'+num+'</span></td>';