onClick: share image on 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 上分享图片


- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 400或500级别的HTTP响应 2022-01-01