简体   繁体   English

双击Facebook分享按钮

[英]Double click on Facebook share button

I have a button to share web content on Facebook on a project and when I click it doesn't works. 我有一个按钮可以在项目上的Facebook上共享Web内容,但单击该按钮不起作用。 The button just works when the users make a double click... :( 当用户双击时,该按钮才起作用... :(

My code: 我的代码:

Html: HTML:

 <button>
    <img src = "images/socialButtons/56/facebook.png" id = "Item-A" onclick="shareFb(this.id)">
 </button>

JavaScript: JavaScript的:

function shareOnFacebook(e, id){
e.preventDefault();
FB.ui(
    {
        method: 'feed',
        name: '',
        link: 'http://anurl.com/'' + id + '.jpg',
        picture: 'http://anurl.com/'+ id +'.jpg',
        caption: '',
        description: "",
        message: ""

    }
);
}

function shareFb(id){

$('#'+ id +'').click(function(e){
    shareOnFacebook(e, id);
});

}

I have implemented the following script on my index.html (the button is on this file) 我在index.html上实现了以下脚本(按钮在此文件上)

<!-- Facebook SDK JavaScript -->
<script>
window.fbAsyncInit = function() {
    FB.init({
        appId      : '#myFacebookAppIdNumber',
        xfbml      : true,
        version    : 'v2.5'
    });
};

(function(d, s, id){
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>

I really appreciate any help ;) 我真的很感谢您的帮助;)

Thanks a lot. 非常感谢。

It takes two clicks because you are attaching a click event after you click it. 单击两次是因为您要在单击事件后附加一个单击事件。 Why do that? 为什么这样 Get rid of the inline click. 摆脱内联点击。

So add a common class: 因此,添加一个普通的类:

<img class="share" id="foo" src="bar.gif" />

Attach the event to the class 将活动附加到班级

$(".share").on("click", function (e) {
    shareOnFacebook(e, this.id);
});

The code above needs to run at the end of your document or on document ready. 上面的代码需要在文档末尾或准备就绪的文档上运行。 You can also use event delegation instead to add just one event handler. 您还可以使用事件委托来仅添加一个事件处理程序。

$(document).on("click", ".share", function (e) {
    shareOnFacebook(e, this.id);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM