将整个元素插入另一个元素,而不仅仅是其内部 HTML

发布于 2024-12-06 15:37:25 字数 671 浏览 1 评论 0原文

我试图弄清楚如何使用 jQuery 尽可能合理地构建 HTML。据我所知,这应该产生

Alice
,但会产生
[object Object]

post = $("<div>");
username = $("<span>").html("Alice");
post.append(username);

我发现用 post.append(username.html()) 替换最后一行可以让我更接近我的目标,但它如果我这样做的话,就会省略 标签。如何插入带有周围标签的子元素,而不写出 "" + username + "",这似乎是完成该任务的新手方法?

编辑:愚蠢的错误。我上面发布的代码片段过于简化;我真的很想在我的代码中执行 post.append(username + another_span_element) 。显然我不能像这样附加对象。我已将其更改为 post.append(username); post.append(another_span_element); 现在它工作正常。杜尔!

I'm trying to figure out how to use jQuery to construct HTML as sanely as possible. As far as I can tell, this should produce <div><span>Alice</span></div>, but instead produces <div>[object Object]</div>:

post = $("<div>");
username = $("<span>").html("Alice");
post.append(username);

I've found that replacing the last line with post.append(username.html()) gets me closer to my goal, but it omits the <span> tags if I do it that way. How do I insert a child element with the surrounding tags, and without writing out "<span>" + username + "</span>", which seems like a novice approach to the task?

EDIT: Stupid mistake. The snippet I posted above was excessively simplified; I was really trying to do post.append(username + another_span_element) in my code. Obviously I can't append objects like that. I've changed it to post.append(username); post.append(another_span_element); and now it works fine. Durr!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

十二 2024-12-13 15:37:25

对我有用: $("

").append($("").html("Alice"))[0].outerHTML == "

< span>爱丽丝

"

Works for me: $("<div>").append($("<span>").html("Alice"))[0].outerHTML == "<div><span>Alice</span></div>"

九命猫 2024-12-13 15:37:25

您的目标是通过 text() 方法完成:

post = $("<div>");
username = $("<span>").text("Alice");
post.append(username);

此处示例

What you're aiming for is done with the text() method:

post = $("<div>");
username = $("<span>").text("Alice");
post.append(username);

Example here.

前事休说 2024-12-13 15:37:25

是否有理由不执行:

$('<div><span>Alice</span></div>').appendTo('body');

var username = "Alice";
$('<div><span id="user"></span></div>').appendTo('body');
$(username).appendTo('#user');

var username = "Alice";
$('<div><span id="user"></span></div>').appendTo('body');
$('#user').html(username);

var username = "Alice";
$('<div><span id="user"></span></div>').appendTo('body');
$('#user').text(username);

或其他 200 个选项中的任何一个?

Is there a reason for not doing:

$('<div><span>Alice</span></div>').appendTo('body');

or

var username = "Alice";
$('<div><span id="user"></span></div>').appendTo('body');
$(username).appendTo('#user');

or

var username = "Alice";
$('<div><span id="user"></span></div>').appendTo('body');
$('#user').html(username);

or

var username = "Alice";
$('<div><span id="user"></span></div>').appendTo('body');
$('#user').text(username);

or any of the other 200 options?

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