【问题标题】:jQuery: Check if a element exists (which could possibly be added through ajax)jQuery:检查元素是否存在(可能通过 ajax 添加)
【发布时间】:2010-12-23 05:46:35
【问题描述】:

我已经知道如何在文档准备好时检查元素:

jQuery.fn.exists = function () { 
  return jQuery(this).length > 0; 
}

但是这个方法不知道使用 AJAX 添加的元素。有人知道怎么做吗?

【问题讨论】:

    标签: javascript jquery ajax dom exists


    【解决方案1】:

    该方法在 ajax 加载并附加到 DOM 后执行。 你可以重写一下:

    jQuery.existsin = function (what, where) { 
      return jQuery(where).find(what).length > 0; 
    }
    

    你可以在 ajax 上取得成功:

    function(data, status){
      if(jQuery.existsin('selector', data)){
        //do foo
      }
    }
    

    【讨论】:

    • 将“body”指定为“默认位置”是否明智?
    • @Eivind 请在这里告诉我选择器。
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-13
      • 2020-04-20
      • 2013-10-28
      • 2011-08-22
      • 2014-02-17
      • 2011-06-26
      相关资源
      最近更新 更多