jQuery.parent()似乎不起作用 [英] jQuery.parent() doesn't appear to be working

查看:86
本文介绍了jQuery.parent()似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.parent()没有返回我指定的父元素.我的代码或html没有任何问题.

.parent() is not returning the parent element that I'm specifying. I don't see any problems with my code or html.

JavaScript:

Javascript:

var vehicle = function () {
    return {
        init: function () {
            var that = this;

            jQuery('.vehicle-year-profile .options .delete').bind('click', function (e) {
                e.preventDefault();
                console.log(e.currentTarget); //  [a.close #]
                that.remove(jQuery(e.currentTarget).parent('.vehicle-year-profile'));
            });
        },
        remove: function (el) {
            console.log(el); // []
            jQuery(el).remove();
        }
    }
}();
jQuery(function() {
    vehicle.init();
});

HTML:

    <div id="vehicle-101031994" class="vehicle-year-profile">
                                        <div class="left">
                                            <h4>1994</h4>
                                        </div>
                                        <div class="right options">
                                            <a class="edit" href="#"><img class="icon-small" src="/image/icons/pencil.png"></a>
                                            <a class="delete" href="#"><img class="icon-small" src="/image/icons/delete.png"></a>
                                        </div>
                                        <div class="clear"></div>
</div>

推荐答案

这是因为

.parents()和.parent()方法类似,不同之处在于后者仅在DOM树上移动单个级别.

The .parents() and .parent() methods are similar, except that the latter only travels a single level up the DOM tree.

您要查找的父级有两个级别,因此您必须使用.parents()(甚至更好的

the parent you are looking for is two levels up, so you'll have to use .parents() (or even better .closest()).

这篇关于jQuery.parent()似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆