如何使用 jQuery 更改 <span> 中的文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7222195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I change the text inside my <span> with jQuery?
提问by Christine Mayes
I have a really simple question but it's something I have not done before. I have the following:
我有一个非常简单的问题,但这是我以前从未做过的事情。我有以下几点:
<td id="abc" style="width:15px;"><span></span></td>
I would like to put some text into the span.
我想在 span 中放入一些文本。
How can I do this with jQuery. I know how to change things with the id of abc but not the code inside the span.
我怎样才能用 jQuery 做到这一点。我知道如何使用 abc 的 id 而不是跨度内的代码更改内容。
thanks,
谢谢,
回答by dexter
$('#abc span').text('baa baa black sheep');
$('#abc span').html('baa baa <strong>black sheep</strong>');
text()
if just text content. html()
if it contains, well, html content.
text()
如果只是文字内容。html()
如果它包含 html 内容。
回答by Alan Haggai Alavi
This will be used to change the Html content inside the span
这将用于更改跨度内的 Html 内容
$('#abc span').html('goes inside the span');
if you want to change the text inside the span, you can use:
如果要更改跨度内的文本,可以使用:
$('#abc span').text('goes inside the span');
回答by scessor
$('#abc span').html('A new text for the span.');
回答by ajup
Try this
尝试这个
$("#abc").html('<span class = "xyz"> SAMPLE TEXT</span>');
$("#abc").html('<span class = "xyz"> SAMPLE TEXT</span>');
Handle all the css relevant to that span within xyz
在 xyz 中处理与该跨度相关的所有 css