在js里拼接html时的一个小小细节
2013-04-18 14:51
549 查看
项目中需要在js里拼接一个table,部分代码如下:
第一种:
第二种:
其中,第二种写法中的onclick和onmouseover事件是执行不出来的,原因在于方法名前少加了一个空格,这样拼接出来的html文档中方法名和前面的属性混在一起,浏览器就无法区分开来,所以当我们在拼接html时一定要想象着它们拼接出来的样子,哪怕多加一个空格 也不要少了一个空格。
最后,空格留在哪儿可以根据自己的习惯,放在某一属性前、后或者单独写成+“ ”+也可以。
var result = " <table style='background-color:#def2f9;' cellspacing='1'>" + " <tr> " + "<td>" + "<table id='table_result'>"; result += "<tr>" + "<th class='td' >员工号</th>" + "<th class='td'>姓名</th>" + "<th class='td'>部门</th>" + "<th class='td'>职位</th>" + "<th class='td'>技能等级</th>" + "<th class='td'>联系电话</th>" + "<th class='td'>籍贯</th>" + "<th class='td'>现住址</th>" + "<th class='td'>在职状态</th>" + "<th class='td'>入职时间</th>" + "<th class='td'>离职时间</th>" + "<th class='td'>身份证号</th>" + "</tr>"; for(var i=0;i<staffViewList.length;i++) { result += "<tr id="+staffViewList[i].staff.staff_id+" onclick='onClickShowDetail(this.id)'"+" onmouseover='changeColor(this);' >"; result += "<td class='td'>"+staffViewList[i].staff.staff_number+" "+"</td>"; result += "<td class='td'>"+staffViewList[i].staff.staff_name+" "+"</td>"; result += "<td class='td'>"+staffViewList[i].dept.dept_name+" "+"</td>"; result += "<td class='td'>"+staffViewList[i].job.job_name+" "+"</td>"; result += "<td class='td'>"+staffViewList[i].level.level_name+" "+"</td>"; result += "<td class='td'>"+staffViewList[i].staff.staff_phone+" "+"</td>"; result += "<td class='td'>"+staffViewList[i].staff.staff_nativeplace+" "+"</td>"; result += "<td class='td'>"+staffViewList[i].staff.staff_address+" "+"</td>"; result += "<td class='td'>"+staffViewList[i].staff.staff_state+" "+"</td>"; result += "<td class='td'>"+timeStamp2String(staffViewList[i].staff.staff_startdate.time)+" "+"</td>"; result += "<td class='td'>"+staffViewList[i].staff.staff_enddate+" "+"</td>"; result += "<td class='td'>"+staffViewList[i].staff.staff_idcard+" "+"</td>"; result += "</tr>"; } result += "</table></td> </tr></table>"; $("div_select_result").innerHTML = result;先看下面两句代码有什么不一样的地方
第一种:
result += "<tr id="+staffViewList[i].staff.staff_id+" onclick='onClickShowDetail(this.id)'"+" onmouseover='changeColor(this);' >";
第二种:
result += "<tr id="+staffViewList[i].staff.staff_id+"onclick='onClickShowDetail(this.id)'"+"onmouseover='changeColor(this);' >";
其中,第二种写法中的onclick和onmouseover事件是执行不出来的,原因在于方法名前少加了一个空格,这样拼接出来的html文档中方法名和前面的属性混在一起,浏览器就无法区分开来,所以当我们在拼接html时一定要想象着它们拼接出来的样子,哪怕多加一个空格 也不要少了一个空格。
最后,空格留在哪儿可以根据自己的习惯,放在某一属性前、后或者单独写成+“ ”+也可以。
相关文章推荐
- JS+html+css实现的一个小小的贪吃蛇游戏
- javascript 的参数有长度限制吗?一个细节引起的误区 分类: 网页编程【html、js】 2009-11-10 08:34 1673人阅读 评论(0) 收藏
- 一个简单好用的工具clipboard.js markdown转html
- 一个B.js获取另一个A.js的方法,只要将在html页面中的载入顺序将A.js放在B.js前即可
- js拼接html字符串注意事项
- 一个html+js+ashx+easyui+ado.net权限管理系统
- js拼接html字符串的注意事项
- javascript再html代码里自动促发js的一个逻辑错误
- CSS+HTML+JS实现的的一个树
- 一个在浏览器端将html 转为pdf 的js 插件 jsPDF
- 在JS的脚本里,怎么把一个html字符串转成的DOM对象.从而能使用返回的DOM对象来访问某一个节点?
- 【原】HTML + XML + JAVASCIPT 实现翻页,跳页功能 ——一个小小英语学习小手册【有注释,有源码】
- html 小小细节...
- js或者cs代码拼接html
- js拼接HTML页面
- [置顶] JS简单原生-定义一个创建HTML列表的方法
- 推荐一个图片管理的html模版和简单的js交互
- 转一个js类型判断函数,支持返回dom 元素的类型,像HTMLBody
- 实验楼一个很好的IT学习平台,包括JAVA,LINUX,HTML,C,CSS,JS等适合初学者