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
}

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: <span id=&quot...
jQuery 添加水印 <script src="../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...