网络整理的选择文字转发到微博的JS,以备后用。直接引入,把需要实现选择转发的标签块改掉就可以实现效果。

var sinaImg = '<img style="display: none; cursor: pointer; position: absolute; width:26px; height:26px;" id="imgSinaShare" title="将选中内容分享到新浪微博" src="http://simg.sinajs.cn/blog7style/images/common/share.gif"/>';
var qqImg = '<img style="display: none; cursor: pointer; position: absolute; width:25px; height:25px;" id="imgQqShare" title="将选中内容分享到腾讯微博" src="http://open.t.qq.com/images/resource/weiboicon32.png">';
document.write(sinaImg);
document.write(qqImg);
var sinaImgShare = document.getElementById("imgSinaShare");
var qqImgShare = document.getElementById("imgQqShare");
var artMain = document.getElementById("share"); //需要实现选择转发到微博的标签块
var $miniBlogShare = function(sinaShare, qqShare, eleContainer) {
var eleTitle = document.getElementsByTagName("title")[0];
eleContainer = eleContainer || document;
var funGetSelectTxt = function() {
var txt = "";
if (document.selection) {
txt = document.selection.createRange().text;
} 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) {
sinaShare.style.display = "inline";
sinaShare.style.left = left + "px";
sinaShare.style.top = top + "px";
qqShare.style.display = "inline";
qqShare.style.left = left + 30 + "px";
qqShare.style.top = top + "px";
} else {
sinaShare.style.display = "none";
qqShare.style.display = "none";
}
};
sinaShare.onclick = function() {
var txt = funGetSelectTxt(),
title = (eleTitle && eleTitle.innerHTML) ? eleTitle.innerHTML: "未命名页面";
if (txt) {
window.open('http://v.t.sina.com.cn/share/share.php?ralateuid=2719305811&title=' + txt + '→来自页面"' + title + '"的文字片段&url=' + window.location.href);
}
};
qqShare.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));
}
};
} (sinaImgShare, qqImgShare, artMain);