9

Facebook 共有を起動する新しい Web サイトの画像に onClick 関数を設定したいと思います。

ここに、私が話していることを理解するのに役立つコードをいくつか示します

<div id="mImageBox">
<img id='my_image' src='' width="auto" height="auto"/>
</div>

「my_image」を使用して別の JavaScript 関数によって挿入されているため、画像 src は空白です

だから、私はこの方法で onClick 関数をセットアップしようとしました:

<script>function fbs_click() {u=location.href;t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;}</script>

<div id="mImageBox">
<a rel="nofollow" href="http://www.facebook.com/share.php?u=<;url>" onclick="return fbs_click()" target="_blank"><img id='my_image' src='' width="auto" height="auto"/></a>
</div>

onClick 関数はうまく機能しますが、img 自体ではなく、img が配置されているページにリンクしています!

何か提案はありますか?

4

3 に答える 3

15

これを試して:

<div id="mImageBox">
<img id='my_image' src='' alt='' width="auto" height="auto" onclick="fbs_click(this)"/>
</div>

<script>
     function fbs_click(TheImg) {
     u=TheImg.src;
     // t=document.title;
    t=TheImg.getAttribute('alt');
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;
}
</script>

JS が無効になっている場合、(<img> タグを囲む) <a> タグを使用する必要はありません。その場合、src属性を挿入する必要がある JS も機能しないためです。

于 2013-06-17T01:31:39.610 に答える
13

以下の関数を呼び出すだけで、Facebook のリンクとテキストで画像を共有できます。

function sharefbimage() {
    FB.init({ appId: `your appid`, status: true, cookie: true });
    FB.ui(
    {
        method: `share`,
        name: 'Facebook Dialogs',
        href: $(location).attr('href'),
        link: 'https://developers.facebook.com/docs/dialogs/',
        picture: 'your image url',
        caption: 'Ishelf Book',
        description: 'your description'
    },
    function (response) {
        if (response && response.post_id) {
            alert('success');
        } else {
            alert('error');
        }
    }
);
于 2015-09-10T10:42:05.183 に答える