js实现将选中内容分享到新浪或腾讯微博
作者:lijiao 时间:2023-08-25 07:39:02
微博如火如荼,大家都选择用微博带来社会化流量,顺便推广产品和网站,几乎所有的网站都有分享到代码,但是还有一种更快捷的分享方式,javascript就可以实现将选定内容轻松分享到新浪微博和腾讯微博,效果图如下:
将选中的内容分享到新浪微博,腾讯微博实现js代码如下:
<STYLE>
.img_sina_share {
DISPLAY: none; CURSOR: pointer; POSITION: absolute
}
.img_qq_share {
DISPLAY: none; CURSOR: pointer; POSITION: absolute
}
</STYLE>
<IMG
class=img_sina_share id=imgSinaShare title=将选中内容分享到新浪微博
src="http://www.phpddt.com/usr/themes/dddefault/images/sina.gif">
<IMG
class=img_qq_share id=imgQqShare title=将选中内容分享到腾讯微博
src="http://www.phpddt.com/usr/themes/dddefault/images/qq.gif">
<SCRIPT>
var eleImgShare = document.getElementById("imgSinaShare"); /
var eleImgShare2 = document.getElementById("imgQqShare");
var $miniBlogShare = function(eleShare,eleShare2,eleContainer) { //实现方法
var eleTitle = document.getElementsByTagName("title")[0];
eleContainer = eleContainer || document;
var funGetSelectTxt = function() { //获取选中文字
var txt = "";
if(document.selection) {
txt = document.selection.createRange().text; // IE
} else {
txt = document.getSelection();
}
return txt.toString();
};
eleContainer.onmouseup = function(e) { //限定容器若有文字被选中
e = e || window.event;
var txt = funGetSelectTxt(), sh = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
var left = (e.clientX - 40 < 0) ? e.clientX + 20 : e.clientX - 40, top = (e.clientY - 40 < 0) ? e.clientY + sh + 20 : e.clientY + sh - 40;
if (txt) {
eleShare.style.display = "inline";
eleShare.style.left = left + "px";
eleShare.style.top = top + "px";
eleShare2.style.display = "inline";
eleShare2.style.left = left + 30 + "px";
eleShare2.style.top = top + "px";
} else {
eleShare.style.display = "none";
eleShare2.style.display = "none";
}
};
eleShare.onclick = function() { //点击新浪微博图标
var txt = funGetSelectTxt(), title = (eleTitle && eleTitle.innerHTML)? eleTitle.innerHTML : "未命名页面";
if (txt) {
window.open('http://v.t.sina.com.cn/share/share.php?title=' + txt + ' ' + title + '&url=' + window.location.href + '','微博分享','width=700, height=580, top=320, left=180, toolbar=no, menubar=no, scrollbars=no, location=yes, resizable=no, status=no');
}
};
eleShare2.onclick = function() { //点击腾讯微博图标
var txt = funGetSelectTxt(), title = (eleTitle && eleTitle.innerHTML)? eleTitle.innerHTML : "未命名页面";
if (txt) {
window.open( 'http://v.t.qq.com/share/share.php?title=' + encodeURIComponent(txt + ' ' + title + ' ') + '&url=' + window.location.href + '','微博分享','width=700, height=580, top=320, left=180, toolbar=no, menubar=no, scrollbars=no, location=yes, resizable=no, status=no');
}
};
}(eleImgShare,eleImgShare2);
</SCRIPT>
将以上的代码黏贴到你想要进行内容分享的页面,要注意的是,最好不要放在header.php或者footer.php这样网站所有内容都进行分享,而我只是放在文章页面!
标签:js,微博,分享
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
分享15 超级好用得 Python 实用技巧
2023-03-01 06:42:09
ASP 使用jqGrid实现读写删的代码(json)
2010-03-07 17:26:00
Pytorch中TensorBoard及torchsummary的使用详解
2023-10-14 11:22:36
![](https://img.aspxhome.com/file/2023/9/64229_0s.png)
Python跑循环时内存泄露的解决方法
2023-07-18 08:18:22
![](https://img.aspxhome.com/file/2023/5/131225_0s.jpg)
PHPMailer发送HTML内容、带附件的邮件实例
2024-05-11 10:07:43
![](https://img.aspxhome.com/file/2023/0/125780_0s.jpg)
Python最基本的数据类型以及对元组的介绍
2022-03-26 07:04:07
MySQL动态修改varchar长度的方法
2024-01-17 22:27:10
.net 6精简版webapi教程及热重载、代码自动反编译演示
2024-05-05 09:13:12
![](https://img.aspxhome.com/file/2023/0/129800_0s.jpg)
详解JavaScript对象的深浅复制
2024-05-11 09:36:30
![](https://img.aspxhome.com/file/2023/1/125951_0s.png)
详解PHP实现HTTP服务器过程
2023-05-26 20:17:12
Python统计学一数据的概括性度量详解
2022-08-16 03:18:20
![](https://img.aspxhome.com/file/2023/4/121364_0s.jpg)
mysql从一张表查询批量数据并插入到另一表中的完整实例
2024-01-15 17:38:43
XMLHttp ASP远程获取网页内容代码
2011-04-10 10:41:00
python爬虫中抓取指数的实例讲解
2022-02-18 04:59:58
![](https://img.aspxhome.com/file/2023/4/101564_0s.png)
Pandas将列表(List)转换为数据框(Dataframe)
2023-02-01 09:48:17
Python实现TCP/IP协议下的端口转发及重定向示例
2021-10-03 04:30:11
![](https://img.aspxhome.com/file/2023/5/103595_0s.png)
Vue+Vux实现登录功能
2024-04-30 10:39:45
![](https://img.aspxhome.com/file/2023/0/130100_0s.png)
Python中的配对函数zip()解读
2021-04-14 20:54:33
python 对excel交互工具的使用详情
2021-11-25 19:10:06
![](https://img.aspxhome.com/file/2023/5/133565_0s.jpg)
JavaScript onclick 和 click 的区别详解
2024-06-05 09:56:25
![](https://img.aspxhome.com/file/2023/0/122140_0s.png)