我在JQuery下面执行.load函数。
$(".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');
}
})请给我建议!
发布于 2010-10-27 11:08:49
您只需将该HTML片段包装到jQuery对象中,并使用它的DOM方法即可。
if( $(response).filter('#tab-container').find('.formContainer').length ) {
// class .formContainer was found
}这里,我使用.filter()方法获取#tab-container div。您不能使用.find(),除非您将完整的片段封装到另一个自创建的div中。在这种情况下,您甚至可以使用$(response).first().find()...,但是.filter()在那里更可靠。
另一种方法(速度更快,但不推荐使用,除非知道您要做什么&传输)是在响应中使用普通的Javascript .indexOf()。
if( response.indexOf('class="formContainer"') > -1 ) {
// class .formContainer was found
}参考资料:$(),.find(),.filter()
https://stackoverflow.com/questions/4032519
复制相似问题