js 拼接字符串带变量(js方法参数单双引号拼接的问题记录)
小结:外面单引号,里面双引号,然后方法参数给转义的单引号即可(看下面的onClick事件即可)
//刷新二级信号表格(增删改操作后) function reloadSignal(subId) { //清空 $("#msgAll"+subId).empty(); //js手工添加表格 var htmlStart= '<span style="position: absolute; top: 6px; right: 55px; font-weight: bold;">'+ '<a style="font-size: 20px" class="handleAddA" onClick="hideMsg(\'msg'+subId+'\')" href="javascript:void(0);">-</a>'+ '</span>'+ '<span style="position: absolute; top: 8px; right: 85px; font-weight: bold;"> <a style="font-size: 18px" class="handleAddA" onClick="showMsg(\'msg'+subId+'\')" href="javascript:void(0);">+</a>'+ '</span>'+ '<span style="position: absolute; top: 12px; right: 5px; font-weight: 400;"> <a class="handleAddA" onClick="manageSubEdit(\''+subId+'\',this)" href="javascript:void(0);">编辑</a>'+ '</span>'+ '<div style="display: none; width: 600px; left: 0px; position: absolute; top: 32px; z-index: 999" id="msg'+subId+'">'+ '<table style="margin: 0 auto; ">'; var htmlMiddle=''; var arr = [1,2,3]; for (var k = 0, length = arr.length; k < length; k++) { htmlMiddle+= '<tr>'+ '<td>'+k+'</td>'+ '<td>'+k+'</td>'+ '</tr>'; } var htmlEnd='</table>'+'</div>'; $("#msgAll"+subId).html(htmlStart+htmlMiddle+htmlEnd); }
样例2
var dydjs = res.split(","); $("#myUl").empty(); dydjs.forEach(function(e) { // 创建一个新的DOM元素 var newElement = $('<li><a href="#" class="jglx" onclick="findData(\''+e+'\')">'+e+'</a></li>'); $("#myUl").append(newElement); });