admin管理员组

文章数量:1244389

From this question I learned that the check mark is the code ✔ (0x2714 [HTML decimal: ✔]). I know that you add text to a span using jQuery by doing $('#spanid').text(texthere);. I want to add the check mark to the span.

I did it like

$('#spanid').text(✔); //This errors on the `&`
$('#spanid').text(#10004); //This results in `Unexpected token ILLEGAL

What is the correct way of doing this?

From this question I learned that the check mark is the code ✔ (0x2714 [HTML decimal: ✔]). I know that you add text to a span using jQuery by doing $('#spanid').text(texthere);. I want to add the check mark to the span.

I did it like

$('#spanid').text(✔); //This errors on the `&`
$('#spanid').text(#10004); //This results in `Unexpected token ILLEGAL

What is the correct way of doing this?

Share Improve this question edited May 23, 2017 at 11:44 CommunityBot 11 silver badge asked May 2, 2015 at 4:28 PekkaPekka 1,1152 gold badges11 silver badges17 bronze badges 4
  • 1 You have to use quotes around that. text('✔') – user4698813 Commented May 2, 2015 at 4:29
  • tried it mate what appeared is the text ✔ not a check mark. I want the check mark to appear not the text – Pekka Commented May 2, 2015 at 4:30
  • Aaah yeah, then use html. – user4698813 Commented May 2, 2015 at 4:31
  • 1 check jsfiddle/2bagqtsv/1 – ketan Commented May 2, 2015 at 4:32
Add a ment  | 

4 Answers 4

Reset to default 6

Use .html(). Also, enclose the value in quotes.

$('#spanid').html('✔');

.text() will convert the input to text string. .html() converts to HTML string/content and the character encoded can be seen.

Fiddle Demo

or if you already have the character , .text() would work;

$('#spanid').text('✔');

What I would do is:

$('#spanid').addClass('check');

and add css;

.check:after {
  content: '(what ever the code for the check mark is)';
}

Alternatively, you could create checkmark with String.fromCharCode:

$('#spanid').text(String.fromCharCode(10004));

Try

$('#spanid').html ('✔');

instead of text(). The text function escapes the string.

本文标签: javascriptUsing jQuery to add a ✔ tick mark in spanStack Overflow