使用Jquery动态添加HTML返回[Object object]

时间:2021-02-11 21:15:45

I am trying to define some basic HTML:

我试图定义一些基本的HTML:

<ul id="menu" style="display:none">
    <li><a class="actions" href="1">1</a></li>
    <li><a class="actions" href="2">2</a></li>
    <li><a class="actions" href="3">3</a></li>
</ul>

And append it dynamically using jquery like so:

并使用jquery动态附加它,如下所示:

$('#container').append($('#menu')).show()

However all that's returned is the physical Object:

然而,所有返回的是物理对象:

<div id="container">[object Object]</div>

Can my menu div be appended in this fashion?

我的菜单div可以这种方式添加吗?

UPDATE: Revised where the show() is placed, but outcome remains the same

更新:修改show()的位置,但结果保持不变

3 个解决方案

#1


1  

While this might work, I would probably run .show() AFTER you append the items to the page. The .append does not expect a function, it's just expecting a DOM element, which may be the cause of your problems.

虽然这可能有效,但我可能会在将项目追加到页面后运行.show()。 .append不期望一个函数,它只是期待一个DOM元素,这可能是你的问题的原因。

#2


1  

This works for me (tried with jQuery 1.6 -> 2.1.3) Did you use a simplified code example in your question? I suspect you are using a string concatenation somewhere like in this thread: Jquery returns [object object] when trying to manipulate dom

这适用于我(尝试使用jQuery 1.6 - > 2.1.3)您是否在问题中使用了简化的代码示例?我怀疑你在这个线程中使用字符串连接:Jquery在尝试操作dom时返回[object object]

#3


0  

This way it should work:

这样它应该工作:

$('#container').append( $('#menu').show() );

#1


1  

While this might work, I would probably run .show() AFTER you append the items to the page. The .append does not expect a function, it's just expecting a DOM element, which may be the cause of your problems.

虽然这可能有效,但我可能会在将项目追加到页面后运行.show()。 .append不期望一个函数,它只是期待一个DOM元素,这可能是你的问题的原因。

#2


1  

This works for me (tried with jQuery 1.6 -> 2.1.3) Did you use a simplified code example in your question? I suspect you are using a string concatenation somewhere like in this thread: Jquery returns [object object] when trying to manipulate dom

这适用于我(尝试使用jQuery 1.6 - > 2.1.3)您是否在问题中使用了简化的代码示例?我怀疑你在这个线程中使用字符串连接:Jquery在尝试操作dom时返回[object object]

#3


0  

This way it should work:

这样它应该工作:

$('#container').append( $('#menu').show() );