如何附加到使用 jQuery 在跨度内添加标签?
我正在尝试将一些文本附加到跨度中的标题中。但我不知道如何附加到实际标题,而不仅仅是跨度。
样式:
h1 {font-size:250%;color:red;}
HTML:
<span class="note">
<h1>
some text
</h1>
</span>
我运行这个:
$('.note:first-child').append("more text");
但是文本被附加在标题标记之后,因此没有应用标题样式。如何附加到标题?
I'm trying to append some text to a heading that is in a span. But I don't know how to append to the actual heading, and not just the span.
Style:
h1 {font-size:250%;color:red;}
HTML:
<span class="note">
<h1>
some text
</h1>
</span>
I run this:
$('.note:first-child').append("more text");
But the text gets appended after the heading tag, and therefore doesn't have the heading style applied to it. How can I append to the heading?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
我正在尝试将一些文本附加到跨度中的标题中。但我不知道如何附加到实际标题,而不仅仅是跨度。
样式:
h1 {font-size:250%;color:red;}
HTML:
<span class="note">
<h1>
some text
</h1>
</span>
我运行这个:
$('.note:first-child').append("more text");
但是文本被附加在标题标记之后,因此没有应用标题样式。如何附加到标题?
I'm trying to append some text to a heading that is in a span. But I don't know how to append to the actual heading, and not just the span.
Style:
h1 {font-size:250%;color:red;}
HTML:
<span class="note">
<h1>
some text
</h1>
</span>
I run this:
$('.note:first-child').append("more text");
But the text gets appended after the heading tag, and therefore doesn't have the heading style applied to it. How can I append to the heading?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
http://jsfiddle.net/bTubp/2/
用于插入第一个
h1<每个
.note
类的 /code>以及
第一个
.note
类的h1
插入内部文本 对于第一个
h1
第一个.note
http://jsfiddle.net/bTubp/2/
for inserting inside the first
h1
of every.note
classand
for inserting inside text for
h1
of first.note
classFor first
h1
of first.note
您需要一个空间让选择器执行您的意思:
或者:
参见演示:
当您说:
.note:first-child
表示具有类注释的东西,也是其父级的第一个子级。当您说:.note :first-child
时,它的意思是某个东西是其父级的第一个子级,并且位于(可能很深)带有类注释的某个东西的内部。当您说:.note>:first-child
时,它的意思是:是其父级的第一个子级,并且其父级具有类注释,这可能就是您的意思。我发现在我写完之前已经有很多答案了,但我仍然会发布它,因为我希望它解释了为什么你的选择器不起作用。
You need a space for your selector to do what you mean:
or:
See DEMOs:
When you say:
.note:first-child
it means something with class note that is also the first child of its parent. When you say:.note :first-child
it means something that is the first child of its parent and is inside (maybe deeply) of something with class note. When you say:.note>:first-child
it means: something that is the first child of its parent and its parent has class note and this is probably what you meant.I see that there are already a lot of answers before I finished writing it but I will still post it because I hope it explains why your selector didn't work.
$(".note h1:first").append("more text");
这将查找类“.note”,后跟第一个 h1 标记。
$(".note h1:first").append("more text");
This would look for the class ".note", followed by the first h1 tag.