[英]jquery: how to append DOM element to selector within variable containing other DOM elements
When storing DOM elements in a javascript variable prior to appending them to the actual DOM is there a way with jQuery to select elements inside the variable?在将 DOM 元素附加到实际 DOM 之前将 DOM 元素存储在 javascript 变量中时,是否可以使用 jQuery 选择变量内的元素?
For example, I have a list of tweets on my page.例如,我的页面上有一个推文列表。 Every time I click a refresh button I append a new tweet to the list below.
每次单击刷新按钮时,我都会在下面的列表中添加一条新推文。 I add the new tweet like follows:
我添加新推文如下:
<li class="tweet normal-tweet" data-user-name="Dorothy">
<div class="image">
<img height="48" width="48" src="http://twitter.com/api/users/profile_image?screen_name=Dorothy" alt="" />
</div>
<div class="content">
<div class="user">
<a class="user-name" href="http://twitter.com/Dorothy" title="Dorothy">Dorothy</a>
<div class="full-name"></div>
</div>
<div class="text">Once in a lullaby</div>
<div class="time-stamp" data-time="1322631934000">1322631934000</div>
</div>
</li>
Inside each tweet on the page, not the new tweet yet, I use jQuery to append some elements.在页面上的每条推文中,还不是新推文,我使用 jQuery 附加一些元素。 I have:
我有:
var actionsMarkup =
"<div class='actions'>
<span class='favorite'>Favorite</span>
<span class='retweet'>Retweet</span>
<span class='reply'>Reply</span>
</div>";
and I append it to the element with the .content class我将它附加到带有 .content 类的元素
$(actionsMarkup).appendTo('#tweets .tweet .content');
Now, when I make a new tweet I do the following:现在,当我发布一条新推文时,我会执行以下操作:
$(document).on('click', '.refresh', function() {
newTweet = $(<new tweet code from above>);
actionsMark = $(actionsMarkup);
$(newTweet.appendTo('#tweets');
I need to be able to append the actionsMark contents to the div with class .content.我需要能够将 actionsMark 内容附加到类 .content 的 div 中。 However, I can't just reapply my prior statement of
但是,我不能只是重新应用我之前的声明
$(actionsMarkup).appendTo('#tweets .tweet .content');
because that puts the markup on all .content divs again, even if it is already there.因为这会将标记再次放在所有 .content div 上,即使它已经存在。
Is there a way for me to use selectors to access the DOM nodes in my newTweet variable before I append it to the document object?在将 newTweet 变量附加到文档对象之前,有没有办法让我使用选择器访问我的 newTweet 变量中的 DOM 节点? I am thinking something like
我在想类似的事情
$(actionsMarkup).appendTo( newTweet, '.content');
If there is no way with selectors to do this then what are some other quick ways?如果选择器无法做到这一点,那么还有哪些其他快速方法?
<ul id="tweets" class="normal-tweet show-promoted-tweets">
<li class="tweet promoted-tweet" data-user-name="Dorothy">
<div class="image">
<img height="48" width="48" src="http://twitter.com/api/users/profile_image?screen_name=Dorothy" alt="" />
</div>
<div class="content">
<div class="user">
<a class="user-name" href="http://twitter.com/Dorothy" title="Dorothy">Dorothy</a>
<div class="full-name">Dorothy</div>
</div>
<div class="text">Somewhere over the rainbow</div>
<div class="time-stamp" data-time="1322631934000">3 minutes ago</div>
</div>
</li>
<li class="tweet promoted-tweet" data-user-name="lion">
<div class="image">
<img height="48" width="48" src="http://twitter.com/api/users/profile_image?screen_name=lion" alt="" />
</div>
<div class="content">
<div class="user">
<a class="user-name" href="http://twitter.com/lion" title="lion">lion</a>
<div class="full-name">Lion</div>
</div>
<div class="text">Way up high,</div>
<div class="time-stamp" data-time="1322631934000">17 minutes ago</div>
</div>
</li>
<li class="tweet normal-tweet" data-user-name="scarecrow">
<div class="image">
<img height="48" width="48" src="http://twitter.com/api/users/profile_image?screen_name=scarecrow" alt="" />
</div>
<div class="content">
<div class="user">
<a class="user-name" href="http://twitter.com/scarecrow" title="scarecrow">scarecrow</a>
<div class="full-name">Scarecrow</div>
</div>
<div class="text">And the dreams that you dreamed of,</div>
<div class="time-stamp" data-time="1322631934000">32 minutes ago</div>
</div>
</li>
</ul>
You can use .last() , to select the last element after you append your new tweet, like below:您可以使用.last()来选择添加新推文后的最后一个元素,如下所示:
$(document).on('click', '.refresh', function() {
newTweet = $(<new tweet code from above>);
$(newTweet).appendTo('#tweets');
var actionsMarkup =
"<div class='actions'>
<span class='favorite'>Favorite</span>
<span class='retweet'>Retweet</span>
<span class='reply'>Reply</span>
</div>";
$("#tweets .tweet").last().find(".content").append(actionsMarkup);
});
if you insist to use appendTo()
, you can try using :last-child :如果你坚持使用
appendTo()
,你可以尝试使用:last-child :
$(actionsMarkup).appendTo('#tweets .tweet:last-child .content');
I was looking to see if you can do the same thing, found this question but the existing answer is not useful in my case as i would like to alter the element before adding.我想看看你是否可以做同样的事情,发现了这个问题,但现有的答案对我来说没有用,因为我想在添加之前更改元素。
You can create a new dom element in a variable with jQuery and do operations on it as if it is already in the dom before appending it.您可以使用 jQuery 在变量中创建一个新的 dom 元素,并在附加之前对其进行操作,就好像它已经在 dom 中一样。
Example:例子:
var newTweet = $('<div></div>');
newTweet.addClass('tweet');
newTweet.append('<span class="username"></span>');
newTweet.find('span.username').html('John Doe');
$('body').append(newTweet);
the following will be appended to the body:以下内容将附加到正文中:
<div class="tweet"><span class="username">John Doe</span></div>
Very handy if you are building a reusable interface element (like a dialogue box) with multiple options.如果您正在构建具有多个选项的可重用界面元素(如对话框),则非常方便。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.