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 }