jQuery 在现有段落标签内追加文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16205788/
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
jQuery append text inside of an existing paragraph tag
提问by sage88
I am trying to add additional text to the end of a paragraph using jQuery. My thought was to produce a structure like this:
我正在尝试使用 jQuery 在段落末尾添加其他文本。我的想法是产生这样的结构:
Judging by my answers I will clarify. I need to create this structure first. And this is the part that I find difficult, not the second end result. So how do I dynamically create this:
从我的回答来看,我会澄清的。我需要先创建这个结构。这是我觉得困难的部分,而不是第二个最终结果。那么我如何动态创建这个:
<p>old-dynamic-text<span id="add_here"></span></p>
And then after adding the new text it would look like this:
然后在添加新文本后,它看起来像这样:
<p>old-dynamic-text<span id="add_here">new-dynamic-text</span></p>
I've been playing with the .wrapAll() method, but I can't quite get the desired effect. Is this the best way to do this (and if so how), or is there another way to append new text to the end of an existing paragraph (that needs to be wrapped in some type of tag since I need to style it differently)?
我一直在玩 .wrapAll() 方法,但我不能完全达到预期的效果。这是执行此操作的最佳方法吗(如果是,如何操作),还是有另一种方法可以将新文本附加到现有段落的末尾(需要将其包装在某种类型的标签中,因为我需要对其进行不同的样式设置) ?
回答by Mahesh Sapkal
回答by Jude Duran
Try this
尝试这个
$('#add_here').text('new-dynamic-text');
回答by ????
I have just discovered a way to append text and its working fine at least.
我刚刚发现了一种附加文本的方法,它至少可以正常工作。
var text = 'Put any text here';
$('#text').append(text);
You can change text according to your need.
您可以根据需要更改文本。
Hope this helps.
希望这可以帮助。
回答by Dipesh Parmar
If you want to append
text or html to span
then you can do it as below.
如果你想append
文本或 html 到span
那么你可以做如下。
$('p span#add_here').append('text goes here');
append will add text to span tag at the end.
append 将在最后添加文本到 span 标签。
to replace entire text or html inside of span you can use .text()
or .html()
要替换跨度内的整个文本或 html,您可以使用.text()
或.html()