onClick:在 Facebook 上分享图片

onClick: share image on Facebook(onClick:在 Facebook 上分享图片)

本文介绍了onClick:在 Facebook 上分享图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的新网站上为启动 Facebook 共享器的图像设置 onClick 功能.

I would like to setup an onClick function to an image on my new website that launch Facebook sharer.

这里有一些代码可以帮助理解我在说什么

Here are some pieces of code that may help understand what I'm talking about

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

图像 src 是空白的,因为它是由另一个 JavaScript 函数使用my_image"注入的

The image src is blank because it's injected by another JavaScript function by using "my_image"

所以,我尝试使用这种方法设置 onClick 功能:

so, I tried to setup the onClick function with this method:

<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="JyB3aWR0aD0="auto" height="auto"/></a>
</div>

onClick函数效果很好,但是它链接到img所在的页面而不是img本身!

The onClick function works well, however, it links to the page where the img is located and not the img itself!

你对我有什么建议吗?

推荐答案

试试这个:

<div id="mImageBox">
<img id='my_image' src="JyBhbHQ9"' 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>

你不需要使用 <a>标记(围绕 <img> 标记)用于 JS 禁用的情况,因为在这种情况下,应该注入 src 属性的 JS 也将不起作用.

You don't need to use <a> tag (surrounding the <img> tag) for case that JS disabled, because in that case the JS that should inject the src attribute will not work also.

这篇关于onClick:在 Facebook 上分享图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:onClick:在 Facebook 上分享图片