如何使用jQuery更改<span>中的文本?


122

我有一个非常简单的问题,但这是我之前从未做过的事情。我有以下几点:

<td id="abc" style="width:15px;"><span></span></td>

我想在文本中输入一些文字。

我该如何使用jQuery。我知道如何更改ID为abc的内容,但不更改范围内的代码。

谢谢,


5
看一下jQuery的选择器文档
Felix Kling

2
@Felix。+100。指向该API的链接比示例代码的“静音”行更有价值。
Roko C. Buljan 2011年

Answers:


178
$('#abc span').text('baa baa black sheep');
$('#abc span').html('baa baa <strong>black sheep</strong>');

text()如果只是文字内容。html()如果包含HTML内容。


6
请注意,<p>inside <span>不适用于所有文档类型。
马达拉的鬼魂

54

这将用于更改跨度内的HTML内容

 $('#abc span').html('goes inside the span');

如果要更改跨度内的文本,可以使用:

 $('#abc span').text('goes inside the span');


13

试试这个

$("#abc").html('<span class = "xyz"> SAMPLE TEXT</span>');

处理xyz中与该跨度相关的所有CSS

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.