JS 分享(微博,微信,QQ ,QQ 空间)

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:45   1175   0
<div>
    <a href="#"  onClick="sinaShare()">微博</a>
    <a href="javascript:void(0);" onClick="wxShare()">微信</a>
    <a href="#"  onClick="qzoneShare()">空间</a>
    <a href="#" onClick="qqShare()">QQ</a>
</div>
function findImg(){
    // 查找内容里面的图片 用于分享时选择图片作为分享图标
    var srcurl='';
    $("div[class=content-detail] img").each(function() {
        srcurl+=$(this).attr("src")+'||';//遇到多张图片时需要使用||分隔
    });
    if(srcurl.length>1){
        return srcurl.substring(0,srcurl.length-2);
    }
    return '';
}
function sinaShare(){
    //新浪微博分享
    var contents=$("#article-title").html();
    var params={
        url:window.location.href,
        title:contents+' —— 转载自: '+$('title').html(),
        pic:findImg()
    };
    var s = [];
    for (var i in params) {
        s.push(i + '=' + encodeURIComponent(params[i] || ''));
    }
    if (contents) {
        window.open("http://v.t.sina.com.cn/share/share.php?"+s.join('&'));
    }
}
function wxShare(){
    //微信分享
    $("#wx-img").attr("src",'http://qr.liantu.com/api.php?text='+encodeURIComponent(window.location.href));
    $("#popBox").css("display","block");
    $("#popLayer").css("display","block");
}

function closeBox(){
    //关闭分享界面
    $("#popBox").css("display","none");
    $("#popLayer").css("display","none");
}

<!--微信分享弹出框-->
<div id="popLayer"></div>
<div id="popBox">
    <div>
        <div>
            <div>分享到微信朋友圈</div>
            <div>
                <a href="javascript:void(0)" onclick="closeBox()">×</a>
            </div>
        </div>
        <img id="wx-img" src=""/>
        <div>打开微信,使用“扫一扫”即可将网页分享至朋友圈或朋友。</div>
    </div>
</div>
function qqShare(){
    //QQ 分享
    shareQQOrQzone("http://connect.qq.com/widget/shareqq/iframe_index.html");
}

function qzoneShare(){
    //QQ 空间分享
    shareQQOrQzone("http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey");
}

function shareQQOrQzone(url){
    //QQ 分享 and QQ 空间分享
    var title1 =$("#article-title").html();
    var contents=$(".content-detail p").html();
    //只截取部分字符串作为引用的内容标题
    contents=contents.replace(/\s*/g,"").replace(/&nbsp;/ig, "").substring(0,90);
    var params={
        url:window.location.href,
        title:'【'+title1+'】 -'+ $('title').html(),
        summary:contents
    };
    var s = [];
    for (var i in params) {
        s.push(i + '=' + encodeURIComponent(params[i] || ''));
    }
    s.push('pics='+findImg());
    if (title1) {
        window.open(url+"?"+s.join('&'));
    }
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP