这有效:
var htmlString = "<div>This is a string.</div>";
这失败了:
var htmlSTring = "<div>
This is a string.
</div>";
有时这对于可读性是可取的。
添加反斜杠以使其工作:
var htmlSTring = "<div>\
This is a string.\
</div>";
或加号
var htmlSTring = "<div>"+
"This is a string."+
"</div>";
或者您还可以查看其他方式并尝试您最喜欢的方式这里
对于您,请查看此代码段或JSFiddle 示例:
$(document).ready(function(){
var opts = [
'Volvo',
'Ford',
'BMW'
];
$("#add").on('click', function(){
htmlString = "<select>";
for (var i in opts) htmlString += "<option>"+opts[i]+"</option>";
htmlString += "</select><input type='text' placeholder='Class #'><br>";
$("#pTags").append(htmlString);
});
});