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

前端之家收集整理的这篇文章主要介绍了jquery – 未捕获的TypeError:j $(…).exists不是函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_404_2@var selector = '#Id-value_' + index; var exist = $(selector).exists();

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

@H_404_2@$(document).ready(function() { });

解决方法

jQuery中没有exists()函数.但你可以快速写一个: @H_404_2@// 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:

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

猜你在找的jQuery相关文章