简体   繁体   English

使用JQuery附加具有多个类名的html元素

[英]Appending an html element with multiple class names using JQuery

I am trying to append this list item <li><i class="fa fa-star" ></i></li> to an unordered list using JQuery $("ul.star-rating").append("<li><i class="fa fa-star" ></i></li>") however I get an error that there a missing ")". 我正在尝试使用JQuery $("ul.star-rating").append("<li><i class="fa fa-star" ></i></li>")将此列表项<li><i class="fa fa-star" ></i></li> $("ul.star-rating").append("<li><i class="fa fa-star" ></i></li>")到无序列表$("ul.star-rating").append("<li><i class="fa fa-star" ></i></li>")但是我收到一个错误,提示缺少“)”。 I assume that I should escape the white-space between the classes "fa"&"fa-star".I don't know if my assumption is correct and I wanted to know how can I achieve such a result: <ul class="star-rating"> <li><i class="fa fa-star"></i></li> </ul> 我假设我应该逃避“ fa”和“ fa-star”类之间的空白。我不知道我的假设是否正确,并且我想知道如何获得这样的结果: <ul class="star-rating"> <li><i class="fa fa-star"></i></li> </ul>

The problem is your use of quotes in this part: 问题是您在这部分中使用了引号:

"<li><i class="fa fa-star" ></i></li>"

You need to escape the "fa fa-star" quotes. 您需要转义"fa fa-star"引号。 Otherwise it's like jamming several different objects together "<li><i class=" , literal fa fa-star and " ></i></li>" . 否则,就像将几个不同的对象"<li><i class=" ,原义的fa fa-star" ></i></li>" fa fa-star在一起。 Or you could just single quotes on the outside. 或者,您也可以在外面加上单引号。

Use 采用

 $("ul.star-rating").append("<li><i class='fa fa-star'></i></li>");

You are using double quotes inside a string that terminates the argument literal of append method. 您在字符串中使用双引号,该字符串终止了append方法的参数文字。

This should work for you. 这应该为您工作。

You have to use single quotes when you start with double quotes or otherwise 以双引号或其他方式开头时,必须使用单引号

$("ul.star-rating").append("<li><i class='fa fa-star'></i></li>")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM