追加并最接近每个div

时间:2017-08-31 10:39:34

标签: jquery

我试图将图像放在最近使用最近的类搜索的div中,但它只是不会附加。

这是jsfiddle:https://jsfiddle.net/o2gxgz9r/13029/

$(".fac").each(function() {
  $(this).closest(".bac").append($(this));
});

2 个答案:

答案 0 :(得分:1)

它实际上不是closest(),而是prev()。如下所示: -

$(".fac").each(function() {
  $(this).prev(".bac").append($(this));
});

您可以使用: -

$(".fac").each(function() {
  $(this).prev().find(".bac").append($(this));
});

答案 1 :(得分:1)

closest不寻找兄弟姐妹。因此,您应该使用prev或使用图像和div的包装器,以便您可以使用closest找到最近的包装器,然后在其中找到div。

此外,您应该使用appendTo将元素附加到div的末尾。

   $("img.fac").each(function() {
                   $(this).appendTo($(this).closest('.wrapper').find('div.bac'));
                });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li id="fli_234" class="col-md-6 col-sm-6 col-xs-6"><a title=" SPORT LINAV HUD"
                                                       href="xom/aine-sunroof-nav-hud-234.html"><img
        alt="BmwPORT LINE SUNROOF NAV HUD"
        src="http://xm/templatesank_10x7.gif"
        data-1x="http://xm/files/11-2016/ad234/863_sold_en.jpg"
        data-2x="http:/xm/files/11-2016245661706047863_sold_en.jpg"
        style="background-image: url(http://wwom/files/11-2016/ad234/15021245661706047863_sold_en.jpg);"
        width="209" height="126"></a>
    <ul class="ad-info">
        <li class="title" title=""><a
                href="xxxx.html">
            3 SeNAV HUD</a></li>
        <li class="services" style="height:20px;"></li>
        <li class="fields"><span id="flf_234_Category_ID">Cars</span><span id="flf_234_condition_tool">Used</span><span
                id="flf_234_carroceria">Sedan</span><span id="flf_234_year_of_production">2013</span><span
                id="flf_234_mileage">28112 km</span></li>
        <li class="two-inline price_tag">
            <nav id="fav_234" class="favorite add" title="Add to Favorites"><span class="icon"></span></nav>
            <div><span class="converted-price">158,413 Lei</span><span class="hide">34,500 €</span></div>
        </li>

    </ul>
</li>
        <div class="wrapper">
 <div class="bac"></div>
<img class="fac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