微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jquery – 如何停止接收:TypeError:$(…).attr(…)未定义?

我正在使用jQuery 1.7.0,并且我一直得到TypeError:$(…).attr(…)是未定义的.有人可以告诉我这是什么问题吗?我的jQuery在下面.谢谢.
function removeNewItem() {
        $('.aboutitemdelete').unbind('click');

        $('.aboutitemdelete').on('click',function () {

            // Get Related Object
            var current = $(this).attr('rel').replace('aboutitemremove_','');

            // Removing Element
            $('#aboutitem_' + current).remove();

            //Assign Current Element Count
            answercount = parseInt($('#answercount').val());
            answercount -= (answercount > 1) ? 1 : 0;
            $('#answercount').val(answercount);
        });
    };

    $(document).ready(function () {
        $('.button_admin_newitem').on('click',function () {

            alert('a');

            current = $('.aboutsectionitem:last').attr('id').replace('aboutitem_','');
            current = Number(++current);

            alert('b');

            $('div.aboutitemsholder').append('<div id="aboutitem_' + current + '" class="aboutsectionitem"><h4 class="titlebar_span">About Page Item</h4><div class="aboutrow_sep"><label>Section Label</label><input id="seclabel_' + current + '" type="text" class="text" /><div class="clear"></div></div><div class="aboutrow_sep"><label>Section Content</label><div class="aboutrow_editor_holder">Telerik Editor Goes Here<div class="clear"></div></div><div class="clear"></div></div><div class="aboutrow_sep"><label>Enabled?</label><input id="itemenable_' + current + '" type="checkBox" class="checkBox" /><div class="clear"></div></div><div class="aboutrow_sep"><label>Sort Order</label><input id="sortitem_' + current + '" type="text" class="text" style="max-width: 50px;" /><div class="clear"></div></div><div class="clear"></div><div class="aboutitemremove"><label>Delete</label><a href="#" class="aboutitemdelete" rel="aboutitemremove_' + current + '"><span>Remove</span></a></div></div>');

            answercount = parseInt($('#answercount').val()) + 1;
            $('#answercount').val(answercount);

            removeNewItem();

            return false;
        });
    });

解决方法

http://api.jquery.com/attr/

As of jQuery 1.6,the .attr() method returns undefined for attributes that have not been set.

我猜你正在搜索属性没有设置在你正在搜索它的元素上.因此,.replace失败,因为undefined没有替换方法.你需要先确保它没有被定义.

var current = $(this).attr('rel');
current = current ? current.replace('aboutitemremove_','') : '';

这也是为下一个区域的id做的.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐