innerHTML 添加标签与转义符
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <div id="bgg">无</div> </body> <script type="text/javascript"> /*反斜杠用来在文本字符串中插入省略号、换行符、引号和其他特殊字符*/ var div = document.getElementById('bgg') function setDiv() { /* * 添加标签,外面里面的引号不能一样; * getTime传参:外面里面的引号不能一样;用"避免这种错误 * 1.onclick="getTime({dateFmt:"2018-04-02"})" " => ";' =>' * 2.onclick="getTime({dateFmt:\'2018-04-02\'})" \' => ' */ div.innerHTML = "<input type='button' onclick='getTime({dateFmt:\"2018-04-02\"})' value='触发事件' />" } this.setDiv() function getTime(obj) { let ob = {} console.info(obj) ob = obj for(let i in ob) { console.info(ob[i]) } } /*两个单引号/双引号一起用报错,需要用转义符转为字符串*/ var name ="错错错" var txt="We are the so-called \"name\" from the north" //true var txt1="getTime({dateFmt:'2018-04-02'})" //true console.info(txt) </script> </html>