错误javascript只获取第一个值数据href



我有一个示例代码:

<div id="fb-root"></div>
<div class="fb-comments" id="fb-comment" data-href="http://fb.com/t1" data-num-posts="3" data-width="590" ></div> 
<div class="fb-comments" id="fb-comment" data-href="http://fb.com/t2" data-num-posts="3" data-width="590" ></div> 

和javascript:

<script>
window.fbAsyncInit = function() {
    FB.init({
        appId : 'xxx', // App ID
        channelUrl : 'xxx', // Channel File
        status : true, // check login status
        cookie : true, // enable cookies to allow the server to access the session
        xfbml : true // parse XFBML
    });
    FB.Event.subscribe('comment.create', function(response) {
        var href = jQuery('.fb-comments').attr('data-href');
        alert(href);
    });
}
</script>

错误jquery只获取第一个值为http://fb.com/t1,不获取http://fb.com/t2

var thestring = '';
$('.fb-comments').each(function () {
    thestring += $(this).attr('data-href') + ' ';
});

演示

使用.each()遍历到fb-comments 类的每个元素

var href='';
jQuery('.fb-comments').each(function () {
    href += $(this).attr('data-href') + ' ';
})

演示

或使用.map()

href是使用以下代码的阵列形式

var href= jQuery('.fb-comments').map(function(){
    return jQuery(this).attr('data-href');
});
console.log(href);

最新更新