在js中拼接HTML页面报错
// js拼接html时onclick,数字可以直接拼接,字符串必须加上引号
οnclick=transferFilePage(1214324,'fihdsih8238');
// js拼接时html,变量需要引号隔开,否则在js中都会认为是字符串(" + opTypeStr + ")
,如果是放在onclick中则是('" + opTypeStr + "')
,其中布尔类型(false)、数字(123)、onclick获取当前对象(this)、不是字符串的变量均不用加引号
transferStr = "<td><div class='ui edit buttons thid'><button class='ui gray button mini' onclick=transferFilePage()>" + opTypeStr + "</button></div>" + overTime + "</td>";
//onclick函数中有多个参数:
"<td>" +
"<div class='ui edit buttons thid'>" +
"<button class='ui gray button mini' οnclick='transferFilePage(" +
"'"+datas[i].report_id+"'," +
"'"+datas[i].temp_id+"'," +
"'"+datas[i].userdeptid+"'," +
"'"+datas[i].status+"'," +
"'"+datas[i].fishId+"'," +
"'"+datas[i].rectifyid+"'," +
"'"+datas[i].transferid+"'," +
"false," + //布尔类型
""+hasRectifyPower+"," + //变量,不是字符串不用加引号('fihdsih8238')
""+hasTransferPower+"," +
"'"+datas[i].adnumber.trim()+"'," +
""+opTypeStr+"," +
""+(datas[i].tempname ? datas[i].tempname.trim() : "")+"," +
"this)'>" +
opTypeStr +
"</button>" +
"</div>" +
overTime +
"</td>";
// JSON字符串
var jsonString = '{"name":"John", "age":30, "city":"New York"}';
// 使用JSON.parse()将其转换为JavaScript对象
var obj = JSON.parse(jsonString);
//HTML中函数传递时注意需要对象转换成字符串
JSON.stringify(obj).replace(/\"/g,"'");
// JSON对象
var obj = {"name":"John", "age":30, "city":"New York"};
var jsonString = JSON.stringify(obj).replace(/\"/g,"'");