简体   繁体   English

.find()和.closest()没有得到我的元素

[英].find() and .closest() not getting my elements

I want to fill a div dynamically suing jQuery. 我想动态地使用jQuery来填充div Now the problem is the following: 现在问题如下:

Using .closest() does not fill my div#articleDetail with divs of classes .date , .views and .count . 使用.closest()没有填满我div#articleDetaildivs.date.views.count Since I believe .closest() climbs up the DOM and not down . 因为我相信.closest() 爬上 DOM和不下来

Only the name in the h4 tags gets filled into the bottom div ( #articledetail ) h4标签中的名称会填充到底部div#articledetail )中

HTML: HTML:

<div id="make" class="span12">
    <article class="make"> <a href=""><img src="../img/175x175.jpg" alt="" /></a>

        <div class="content">
             <h4><a class="merc" href="#latestClick">Mercedes-Benz</a></h4>

            <div class="date">12 March 2013</div>
            <div class="count"><span></span> photos</div>
            <div class="views">Views: <span>32</span>
            </div>
        </div>
    </article>
    <article class="make latest"> <a href=""><img src="../img/175x175.jpg" alt="" /></a>

        <div class="content">
             <h4><a class="bmw" href="#latestClick">BMW</a></h4>

            <div class="date">12 March 2013</div>
            <div class="count"><span></span> photos</div>
            <div class="views">Views: <span>89</span>
            </div>
        </div>
    </article>
</div>
<div id="articleDetail"></div>

JS: JS:

$('#make article h4 a').click(function (e) {
    e.preventDefault();
    $('#latestInner, #make, #models').hide(200);
    $('#article').show(500);
    //fill articleDetail (name, date, views, count)
    $('#articleDetail').empty();
    $(this).closest('.date').clone().appendTo('#articleDetail'); //date
    $(this).closest('.count').clone().appendTo('#articleDetail'); //count
    $(this).closest('.views').clone().appendTo('#articleDetail'); //views
    $(this).parent().clone().appendTo('#articleDetail'); //name
.....

Any suggestions how I can achieve this? 有什么建议可以实现这一目标吗?

NOTE: I do NOT want to add ID's and I do not want to change the structure of my HTML. 注:希望添加ID的,我不想改变我的HTML的结构。 There has to be a way to call and append the appropriate classes to #articledetail . 必须有一种方法来调用适当的类并将其附加到#articledetail

.views .count and .date

You are right .closest goes up the tree, not down. 您是对的。 .closest是上树,而不是下树。 The elements you want to find are all children of the link's grandparent. 您要查找的元素都是链接的祖父母的所有子元素。 So what do you do? 所以你会怎么做? You find the grandparent first and then search its children: 您首先找到祖父母,然后搜索其子代:

var $content = $(this).closest('.content');
$content.find('.date').clone().appendTo('#articleDetail');
// ...

closest finds the closest ascendant that matches the given selector. closest查找与给定选择器匹配的最接近的上升点。 However in your markup <div class="date"> is not an ancestor of a element, and therefore nothing is found. 然而,在您的标记<div class="date">不是一个祖先a元件,因此没有被发现。 What should work though is something like: 但是应该起作用的是这样的:

$(this).closest('.content').find('.date')

只需爬到包装纸,然后在其中找到东西即可:

$(this).closest('.content').find('.date.')

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

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