简体   繁体   English

使用jQuery只定位一个元素

[英]Target only one element with jQuery

I have this little problem with my code, and I though guys at Stack Overflow might know how to solve this. 我的代码有这个小问题,尽管我在Stack Overflow的家伙可能知道如何解决这个问题。

So here's the scenario: 所以这是场景:

I need to append strong inside p into another element which will be post caption. 我需要将strong内部p附加到另一个元素上,该元素将是标题。 I have been able to create solution this, but I'm having problem. 我已经能够创建解决方案,但是遇到了问题。 Photo caption duplicates into other same named elements, which I don't want to happen. 图片标题会复制到其他相同的命名元素中,我不想发生这种情况。 So post may have photo caption, but not always, but it still needs to work. 因此,发布可能带有图片说明,但并非总是如此,但仍需要工作。

HTML 的HTML

<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 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>");

Here's Codepen snippet: 这是Codepen代码段:

http://codepen.io/aleksitappura/pen/auvrE http://codepen.io/aleksitappura/pen/auvrE

I'll appreciate all of your help. 多谢您的协助。

For multiple strong tags, you can try this demo: 对于多个strong标签,您可以尝试以下演示:

http://jsfiddle.net/lotusgodkk/7mP8Y/1/ http://jsfiddle.net/lotusgodkk/7mP8Y/1/

JS: 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 演示

You need to save a reference to the source so that you can return the caption there: 您需要保存对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>");

UPDATE 更新

In case you're targeting more than one element, you can use .each as follows and each element will be changed on it's own: 如果您要定位多个元素,则可以按以下方式使用.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>");
});

$(".post strong:first-child").parent().remove(); indicates the first strong in each .post . 指示每个.post的第一个strong

If you only wanted the first .post you'd do $('.post').first().find('strong'); 如果只想要第一个.post可以执行$('.post').first().find('strong'); or $('.post:first-child strong'); $('.post:first-child strong');

Update your jquery code like below. 如下更新您的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();

DEMO 演示

EDIT 编辑

If you want to do this for all the post section then you have to use each function like below. 如果要对所有帖子部分执行此操作,则必须使用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();
 });

DEMO2 演示2

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

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