对象不支持最接近'的属性或方法。

时间:2016-03-22 14:44:13

标签: jquery internet-explorer microsoft-edge

我得到的信息是我的jQuery函数不能在IE上工作,也没有Edge。在控制台中我有消息:

对象不支持属性或方法'最近'

这是jQuery:

$('body').on('change', 'select', function (event) {
    if(event.target.id.indexOf("couche") >= 0) {
        $.ajax({
            url: "{{ redir2 }}",
            type: "POST",
            data: {
                ident: event.target.id,
                value: event.target.value,
                iscouche: "True"
            },
        }).done(function (msg) {
            if(msg.nothing == 1) {
                var what = event.target.closest('tbody');
                $(what).find("tr:gt(0)").remove();
            } else {
                var add = event.target.closest('tr');
                var toremove = msg.toremove.split(" ");
                for(var i = 0; i < toremove.length; i++) {
                    if(toremove[i].length > 0) {
                        jQuery(toremove[i]).remove();
                    }
                }
                jQuery(add).after(msg.ret);
            }
        });

    } else {
        $.ajax({
            url: "{{ redir2 }}",
            type: "POST",
            data: {
                ident: event.target.id,
                value: event.target.value,
                iscouche: "False"
            },
        }).done(function (msg) {});
    }
});

有人可以告诉我是否有解决方法?

3 个答案:

答案 0 :(得分:14)

closest()是在jQuery原型上定义的,它不能用在普通的JavaScript对象上。

event.target是发生事件的DOM元素,要在其上使用jQuery方法,该元素需要包装在jQuery中。

更改

var what = event.target.closest('tbody')

var what = $(event.target).closest('tbody')

答案 1 :(得分:14)

event.target是一个DOM节点,而不是一个jQuery对象,所以没有jQuery方法

在jQuery中使用$(this)这是一个jQuery对象。

如果您不需要,我还建议您不要使用目标。

更新:较新的浏览器现在拥有DOM方法closest,因此OP代码可以在IE以外的新浏览器中使用。

这是一个固定的jQuery版本:

$('body').on('change', 'select', function(event) {
  var $sel = $(this),    // the changed select
    id = this.id,        // or $(this).attr("id"); 
    val = $(this).val(); // or this.value
  if (id.indexOf("couche") >= 0) {
    $.ajax({
      url: "{{ redir2 }}",
      type: "POST",
      data: {
        ident: id,
        value: val,
        iscouche: "True"
      },
    }).done(function(msg) {
      if (msg.nothing == 1) {
        var what = $sel.closest('tbody')
        $(what).find("tr:gt(0)").remove();
      } else {
        var add = $sel.closest('tr');
        var toremove = msg.toremove.split(" ")
        for (var i = 0; i < toremove.length; i++) {
          if (toremove[i].length > 0) {
            jQuery(toremove[i]).remove();
          }
        }
        jQuery(add).after(msg.ret);
      }
    });

  } else {
    $.ajax({
      url: "{{ redir2 }}",
      type: "POST",
      data: {
        ident: id,
        value: val;,
        iscouche: "False"
      },
    }).done(function(msg) {});
  }
});

或更整洁:

$('body').on('change', 'select', function(event) {
  var $sel = $(this), // the select changed
    val = this.value,
    id = this.id,
    isCouche = id.indexOf("couche") != -1;
  $.ajax({
    url: "{{ redir2 }}",
    type: "POST",
    data: {
      ident: id,
      value: val,
      iscouche: isCouche ? "True" : "False";
    },
  }).done(function(msg) {
    if (isCouche) {
      if (msg.nothing == 1) {
        var what = $sel.closest('tbody')
        $(what).find("tr:gt(0)").remove();
      } else {
        var add = $sel.closest('tr');
        var toremove = msg.toremove.split(" ")
        for (var i = 0; i < toremove.length; i++) {
          if (toremove[i].length > 0) {
            $(toremove[i]).remove();
          }
        }
        $(add).after(msg.ret);
      }
    } else {
      // handle not couche
    }
  });
});

答案 2 :(得分:2)

Yuo必须在$()中包含它,因为event.target不是jQuery元素

   $(event.target).closest('tr')