正如其他评论者所建议的那样,最有效的方法似乎是:
if ($(selector).length ) { // Do something}如果您绝对必须具有一个exist()函数-这会变慢-您可以执行以下操作:
jQuery.fn.exists = function(){return this.length>0;}然后,您可以在代码中使用
if ($(selector).exists()) { // Do something}
正如其他评论者所建议的那样,最有效的方法似乎是:
if ($(selector).length ) { // Do something}如果您绝对必须具有一个exist()函数-这会变慢-您可以执行以下操作:
jQuery.fn.exists = function(){return this.length>0;}然后,您可以在代码中使用
if ($(selector).exists()) { // Do something}