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

jquery – 未捕获的TypeError:j $(…).exists不是函数

var selector = '#Id-value_' + index;
var exist = $(selector).exists();

我收到这段代码错误.
我的文档就绪功能

$(document).ready(function() {

});

解决方法

jQuery中没有exists()函数.但你可以快速一个
// Add a new function to jQuery
jQuery.fn.exists = function(){ return this.length > 0; }

// Sadly,we cannot use ES6 arrow functions here. It
// would be nice if we Could do this:
// jQuery.fn.exists = () => this.length > 0;

//Now let's test it
if ($(selector).exists()) {
    // Do something
}

或者,只检查.length属性不等于0:

if ($(selector).length) {
  // will go here if at least one node matched
}

原文地址:https://www.jb51.cc/jquery/175833.html

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

相关推荐