使用jQuery的多行string插入

$("#addSelect").click(function() { $("#optionsForm").after("Hello world."); } ); 

这工作。

 $("#addSelect").click(function() { $("#optionsForm").after("<tr> <td><input type="text" class="optionField" value="Options" /></td> <td> <ul class="option"> <li><select><option>Value..</option></select></li> </ul> </td> </tr>"); } ); 

像这样的东西不。

在Chrome中,我收到错误“ Unexpected token ILLEGAL ”。 谷歌search后,我发现我的小脑不知道多lessJavaScript和多行。 所以我在每一行的末尾添加了'\'。 然而,我现在得到错误“ 意外的标识符 ”。

我想这不像我做的那样困难:)

将所有属性的双引号更改为单引号。

 $("#addSelect").click(function() { $("#optionsForm").after("<tr> \ <td><input type='text' class='optionField' value='Options' /></td> \ <td> \ <ul class='option'> \ <li><select><option>Value..</option></select></li> \ </ul> \ </td> \ </tr>"); } ); 

更简洁的方法是使用<script>标签

https://stackoverflow.com/a/12097933/1416458

 <script id="stuff_you_want" type="text/plain"> <tr> <td><input type="text" class="optionField" value="Options" /></td> <td> <ul class="option"> <li><select><option>Value..</option></select></li> </ul> </td> </tr> </script> <script> // pure javascript var text = document.getElementById("stuff_you_want").innerHTML ; //or using jQuery... (document ready for safety) $(document).ready(function() { var text = $("#stuff_you_want").html(); } </script> 

内容types必须设置为符合html 4.0 。