简体   繁体   English

如何在div中更改div的内容?

[英]how do i change contents of a div within a div?

What I am trying to do is change the contents of a div within a div. 我正在尝试做的是在div中更改div的内容。 I can't seem to access it. 我似乎无法访问它。

dialog is the parent while dialogChange is the child. 对话框是父级,而dialogChange是子级。

When I do: 当我做:

$("#dialog").text("New Text"); $(“#dialog”)。text(“ New Text”);

It'll replace everything within the parent, dialog. 它将替换父对话框中的所有内容。

But when I do: 但是当我这样做时:

$("#dialogChange").text("New Text"); $(“#dialogChange”)。text(“ New Text”);

Nothing changes. 没有什么变化。

So how can I access the child within a parent? 那么,如何在父母中与孩子接触?

If you do $("#dialog").text("New Text"); 如果您执行$("#dialog").text("New Text"); , this will effectively remove dialogChange, so $("#dialogChange").text("New Text"); ,这将有效地删除dialogChange,因此$("#dialogChange").text("New Text"); won't work. 将无法正常工作。

Instead of using the text method, which replaces the entire contents of the div, you should try some of these methods: 而不是使用替换div整个内容的text方法,您应该尝试以下方法:

.before()
.after()
.append()

For example, you could use $("#dialogChange").before("New Text") to insert something before that div. 例如,您可以使用$("#dialogChange").before("New Text")在该div之前插入内容。

Play around for the exact effect you want, and use the API as a guide: 试一试您想要的确切效果,并使用API​​作为指导:

http://api.jquery.com/ http://api.jquery.com/

这样的事情怎么样:

$('parent').find('child').text('New Text');

Your jQuery looks good. 您的jQuery看起来不错。 Possibly the problem is in your HTML. 问题可能出在您的HTML中。

Or by any chance have your replaced the contents of the parent (and therefore deleted the child) before you call the code affecting the child? 还是有机会在调用影响孩子的代码之前替换了父对象的内容(因此删除了孩子)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM