使用jQuery只瞄准一个元素
Target only one element with jQuery
我的代码有这个小问题,我想Stack Overflow的家伙可能知道如何解决这个问题。
情况是这样的:
我需要在p
内附加strong
到另一个元素,这将是帖子标题。我已经能够创建解决方案,但我有问题。图片说明复制到其他相同的命名元素,我不希望发生。所以post可能有图片说明,但并不总是这样,但它仍然需要工作。
<div class="post">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut ornare erat sit amet nulla faucibus pharetra. Donec in mauris lorem. Pellentesque egestas aliquet lobortis. Praesent molestie.</p>
</div>
<div class="post">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut ornare erat sit amet nulla faucibus pharetra. Donec in mauris lorem. Pellentesque egestas aliquet lobortis. Praesent molestie.</p>
</div>
<div class="post">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut ornare erat sit amet nulla faucibus pharetra. Donec in mauris lorem. Pellentesque egestas aliquet lobortis. Praesent molestie.</p>
<p><strong>This is a caption.</strong></p>
</div>
JS
var $caption = $(".post strong:first-child").text();
var $removable = $(".post strong:first-child");
$(".post strong:first-child").parent().remove();
$removable.remove();
$(".post").append("<p class='post-caption'>" + $caption + "</p>");
代码片段:
http://codepen.io/aleksitappura/pen/auvrE 我很感激你的帮助。$(".post strong:first-child").parent().remove();
表示每个.post
中的第一个strong
。
如果你只想要第一个.post
,你可以选择$('.post').first().find('strong');
或$('.post:first-child strong');
对于多个strong
标记,您可以尝试以下演示:
JS:
var i = $(".post strong:first-child");
i.each(function () {
var elem = $(this);
var post = elem.parents('.post:first');
var $caption = elem.text();
elem.parent().remove();
post.append("<p class='post-caption'>" + $caption + "</p>");
});
DEMO
你需要保存一个引用到source
,这样你就可以在那里返回标题:
var $caption = $(".post strong:first-child").text();
var $removable = $(".post strong:first-child");
//SAVE A REFERENCE TO THE SOURCE/TARGET
var $source = $removable.closest('.post');
$(".post strong:first-child").parent().remove();
$removable.remove();
//ADD TO SOURCE/TARGET
$source.append("<p class='post-caption'>" + $caption + "</p>");
如果你的目标是一个以上的元素,你可以使用.each
如下所示,每个元素都会自己改变:
$(".post strong:first-child").each(function() {
var $caption = $(this).text();
var $parent = $(this).closest('.post');
$(this).parent().remove();
$parent.append("<p class='post-caption'>" + $caption + "</p>");
});
按如下方式更新jquery代码
var $caption = $(".post strong:first-child").text();
var $removable = $(".post strong:first-child");
$(".post strong:first-child").parent().append("<p class='post-caption'>" + $caption + "</p>");
$removable.remove();
编辑
如果你想在所有的post部分都这样做,那么你必须使用each
函数,如下所示。
$(".post").each(function(){
var $caption = $(this).find("strong:first-child").text();
$(this).find("strong:first-child").parent().append("<p class='post-caption'>" + $caption + "</p>");
$(this).find("strong:first-child").remove();
});
以及接下来
相关文章:
- 使用clickToggle并在单击另一个元素时关闭元素
- 表追加而不附加最后一个元素
- 滚动到容器中的下一个元素-几乎到了
- 使用CSS或js,使用动态选择器选择任意li的下一个元素
- 按我自己的类克隆另一个元素的内容和顺序
- Javascript获取上一个元素的内容
- 如何通过HTML+CSS中的另一个元素Selector更改元素的属性值
- 在数组中的一个元素上设置多个值
- 无法选择除一个元素外的所有元素
- 使用jquery contains获取一个元素并添加类
- $(.class).empty总是缺少一个元素
- jQuery-将列表项的一个元素移动到同一项的另一个元素中+对我的列表中的每个项执行此操作
- 如何在一个元素动画之后延迟
- jQuery转盘下一个元素没有滑入
- jquery从另一个元素中移除移除
- DIV怎么能像Javascript中的另一个元素一样工作呢
- 当知道同一hiearch中至少有一个元素时,遍历到元素.结构使用jquery
- 使用数据属性将HTML数据复制到另一个元素
- 如何选择给定类的上一个元素
- 动态修改一个元素,使其与给定的选择器匹配