2010-10-27 21 views
0

我在JQuery下面執行.load函數。如何在jquery中的響應文本中檢查現有的.class

$(".load-fragment").each(function()   
     {   
      var $objThis = $(this); 
      var fname = $objThis.attr("href"); //Getting the href of the element 
      var dynDivID = "divContent"+ $objThis.attr("id"); //Name of the dynamic div ID 
      var newDiv = $("<div>").attr("id",dynDivID) 
      .load(fname+ " #tab-container", {pupdate:"true"},function(response, status, xhr) 
      { 
       if ($(response+".formContainer").length) 
       { 
        $objThis.removeClass('load-fragment');              
       }  
       if (status == "error") 
       {    
        newDiv.removeClass('dynDiv'); 
        newDiv.addClass('errorDiv'); 
       } 
      })//Loading page fragment from the given link 
      .hide()//Hiding all the newly created DIVs 
      .addClass('dynDiv')//Adding CSS to newly created Dynamic Divs 
      .append($('<img/>').attr({ src: '/system/Images/ajax-loader-circle-thickbox.gif', alt: '', style:'margin:50px 0px 50px 185px' }));//Adding the loading.gif file 
      $("#container-4").append(newDiv);//adding new div in div column2 
     }); 

在上面一.load功能,我想從鏈接的頁面加載頁面片段。我以下面的html格式獲得響應。

<div class="tabs-container" id="tab-container"> 
    <div class="contentContainer"> 
     <div class="contentContainer"> 
      <p> 
       Book your New Delhi flights with Emirates and experience our award-winning service 
       flying direct to Australia's most iconic city.</p> 
     </div> 
    </div> 
    <div class="formContainer"> 
     <p>Testing</p> 
    </div> 
</div> 

現在我想回應的文本,以檢查是否有響應內部存在或不formContainer類,然後執行某些工作。下面是我正在嘗試的代碼,但它不適合我。

.load(fname+ " #tab-container", {pupdate:"true"},function(response, status, xhr) 
       { 
        if ($(response+".formContainer").length) 
        { 
         $objThis.removeClass('load-fragment');              
        }  
        if (status == "error") 
        {    
         newDiv.removeClass('dynDiv'); 
         newDiv.addClass('errorDiv'); 
        } 
       }) 

請建議!

回答

1

您可以將該HTML代碼片段包裝到一個jQuery對象中並使用它的DOM方法。

if($(response).filter('#tab-container').find('.formContainer').length) { 
    // class .formContainer was found 
} 

這裏我使用的是.filter()方法搶#tab-container股利。除非您將完整的片段包裝到另一個自創的div中,否則您不能使用.find()。在這種情況下,您甚至可以使用$(response).first().find()...,但.filter()在那裏更可靠。

另一種方式(這是更快,但推薦,除非確切地知道你做了什麼&轉移)是在您的迴應中使用一個普通的JavaScript .indexOf()

if(response.indexOf('class="formContainer"') > -1) { 
    // class .formContainer was found 
} 

參考:$().find().filter()

+0

感謝安迪,但如果我要檢查裏面的div#標籤容器下方響應,僅僅是從我的最終 – 2010-10-27 11:12:18

+0

@Solution更安全的東西:更新 – jAndy 2010-10-27 11:15:41

+0

很多謝謝,但過濾器不工作,但找到工作,而不是 – 2010-10-27 11:18:58

相關問題