如何附加到

使用 jQuery 在跨度内添加标签?

发布于 2024-10-20 04:16:13 字数 451 浏览 1 评论 0原文

jsFiddle

我正在尝试将一些文本附加到跨度中的标题中。但我不知道如何附加到实际标题,而不仅仅是跨度。

样式:

h1 {font-size:250%;color:red;}

HTML:

<span class="note">
    <h1>
        some text
    </h1>
</span>

我运行这个:

$('.note:first-child').append("more text");

但是文本被附加在标题标记之后,因此没有应用标题样式。如何附加到标题?

jsFiddle

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷月断魂刀 2024-10-27 04:16:13

http://jsfiddle.net/bTubp/2/

$('.note h1:first-child').append("more text");

用于插入第一个 h1<每个 .note 类的 /code>

以及

$('.note:first-child h1').append("more text");

第一个 .note 类的 h1 插入内部文本 对于

第一个 h1 第一个 .note

$('.note:first-child h1:first-child').append("more text");

http://jsfiddle.net/bTubp/2/

$('.note h1:first-child').append("more text");

for inserting inside the first h1 of every .note class

and

$('.note:first-child h1').append("more text");

for inserting inside text for h1 of first .note class

For first h1 of first .note

$('.note:first-child h1:first-child').append("more text");
命硬 2024-10-27 04:16:13

您需要一个空间让选择器执行您的意思:

$('.note :first-child').append("more text");

或者:

$('.note > :first-child').append("more text");

参见演示:

当您说: .note:first-child 表示具有类注释的东西,也是其父级的第一个子级。当您说: .note :first-child 时,它的意思是某个东西是其父级的第一个子级,并且位于(可能很深)带有类注释的某个东西的内部。当您说: .note>:first-child 时,它的意思是:是其父级的第一个子级,并且其父级具有类注释,这可能就是您的意思。

我发现在我写完之前已经有很多答案了,但我仍然会发布它,因为我希望它解释了为什么你的选择器不起作用。

You need a space for your selector to do what you mean:

$('.note :first-child').append("more text");

or:

$('.note > :first-child').append("more text");

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.

昇り龍 2024-10-27 04:16:13
$('.note:first-child h1').append("more text");
$('.note:first-child h1').append("more text");
不一样的天空 2024-10-27 04:16:13

$(".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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文