2
我使用jQuery中的find
方法在页面上查找特定框。在jQuery中实时查找
我有其他的按钮,创建新的盒子没有页面刷新。这些新盒子然后没有找到与find
,这是一个问题。
我用:
$("fieldset#"+new_item_id).find("div.images").append(img);
,但我想怎样也实现了live
方法。或者我应该使用on
还是其他?这个实时更新的东西是艰难的。
我使用jQuery中的find
方法在页面上查找特定框。在jQuery中实时查找
我有其他的按钮,创建新的盒子没有页面刷新。这些新盒子然后没有找到与find
,这是一个问题。
我用:
$("fieldset#"+new_item_id).find("div.images").append(img);
,但我想怎样也实现了live
方法。或者我应该使用on
还是其他?这个实时更新的东西是艰难的。
你将不得不在每个事件上调用一个函数来实现这一点。
function addImg(new_item_id, img){
$("fieldset#"+new_item_id).find("div.images").append(img);
}
对于那些已经存在的元素,您必须在页面加载时调用它。对于每个添加的元素,您再次调用它。所以,你最终的东西,如:
$(function(){
$("fieldset[id]").each(function(){
var img = //how ever you find this image...
addImg($(this).attr('id'),img);
});
$('button').click(function(){
//some ajax call
$.ajax(
// what ever options you have for url, data etc etc.
success: function(response){ // assuming response is just the markup
var $el = $(response);
$('#content').append($el); // how ever you add this content - its probably NOT #content but you'll know that...
var img = //how ever you find this image...
addImg($el.attr('id'), img);
}
);
});
});
function addImg(new_item_id, img){
$("#"+new_item_id).find("div.images").append(img);
}
编辑 - 而不是甲肝功能找到的元素只是通过它...
$(function(){
$("fieldset[id]").each(function(){
var img = //how ever you find this image...
addImg($(this),img);
});
$('button').click(function(){
//some ajax call
$.ajax(
// what ever options you have for url, data etc etc.
success: function(response){ // assuming response is just the markup
var $el = $(response);
$('#content').append($el); // how ever you add this content - its probably NOT #content but you'll know that...
var img = //how ever you find this image...
addImg($el, img);
}
);
});
});
function addImg($newEle, img){
$newEle.find("div.images").append(img);
}
你不应该使用.live,这是过时的 – Sergio 2013-03-14 11:14:15
哦,那么呢?谢谢我不知道 – Steeven 2013-03-14 11:14:42
'$(“fieldset#”+ new_item_id)'它超过合格,意味着没有必要在它的前面添加'fieldset' – Val 2013-03-14 11:15:48