我需要帮助来简化我的AJAX代码。我是一个新手,我确信我的方法是错误的,错误的,错误的。但我不确定如何纠正我的错误并妥善处理。
要点:
问题:有没有办法加强这一点,以便一个代码段可以应用于页面上的所有拇指?
<div id="gallery-wrapper">
<div class="thumb" id="thumb_75">
<img src="/thumbs/thumb_75.jpg" alt="Thumb 75">
<a href="/url/to/hide_thumb.php" id="hide_75">Delete</a>
</div>
<script>
$('#hide_75').click(function(e){e.preventDefault();
$.post($(this).attr("href"));
$('#thumb_75').hide(500);
});
</script>
</div>
ALSO:我注意到,当第一批拇指(使用无限滚动)添加/附加新拇指时,上述隐藏功能对附加的拇指不起作用 - 仅在开头加载的原始拇指。
我很确定我需要“告诉”jQuery这些新的拇指已被添加,但由于上面的代码是如此foobar'd我不知道如何/从哪里开始。
My Infinite Scroll代码包含在页面的最底部,如下所示:
//Infinite scroll
$wall.infinitescroll({
navSelector : 'div.pagination',
nextSelector : 'div.pagination a.more',
itemSelector : '.thumb',
loading: {
msgText: "Loading more thumbs...",
finishedMsg: "That's all folks.",
img: "/graphics/loading.gif"
},
animate : true,
extraScrollPx: 150,
debug: true,
errorCallback: function() {
$('#infscr-loading').animate({opacity: 0},2500);
}
},
function( newElements ) {
var $newElems = $( newElements ).css({ opacity: 0 });
$newElems.imagesLoaded(function(){
///// PRETTY SURE I NEED TO DO SOMETHING HERE TO INFORM jQUERY OF THE NEW ITEMS
$newElems.animate({ opacity: 1 });
$wall.masonry( 'appended', $newElems, true );
}
});
});
谢谢。
答案 0 :(得分:2)
尝试以下方法:
// add a listener on the gallery-wrapper. All clicks on links inside the
// wrapper will be caught here. This way, new thumbs which are added later
// are also covered, because you aren't listining on every child but on
// one static container.
$('#gallery-wrapper').on('click', 'a', function (e) {
// prevent default action
e.preventDefault();
// get the current scope, this is the 'a'-element
var $this = $(this);
// post to the url
$.post($this.attr('href'));
// find container of current thumbnail and hide
$this.closest('.thumb').hide(500);
});