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
}