weixin_33699914 2012-09-14 15:10 采纳率: 0%
浏览 34

.click()在循环内

I have this in my wordpress post loop:

function newWindow(uri,width,height) {
    if(!window.open(uri,uri,'scrollbars=1,toolbar=0,resizable=1,status=0,width='+width+',height='+height)) {
        document.location.href=uri;
    }
}
$('.facebook_button').click(function() {
    newWindow('http://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>',720,420);
    return false; 
});

so when I click the button, it opens several windows (10 for 10 posts on the index page). Is there a way to open only for post the button was in?

Thanks!

  • 写回答

1条回答 默认 最新

  • weixin_33690963 2012-09-14 15:20
    关注

    You should not have the javascript function definition inside the loop (it only needs to be output once).

    I would suggest adding a unique id (maybe a hash of the_permalink() value) to each item in the loop, so you give yourself a good handle for the selector. So you would output in the loop something like

    $('#<?php echo md5(get_permalink()); ?>').click(function() {
        newWindow('http://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>',720,420);
        return false; 
    });
    

    Of course you have to add id="<?php echo md5(get_permalink()); ?>" also to the buttons you output.

    评论

报告相同问题?