随机显示微信号,并复制跳转微信

本文介绍了一种使用HTML、CSS和JavaScript实现随机显示多个微信号,并提供复制和跳转至微信功能的方法。通过设置定时器,页面上的微信号会定期更新,确保每次查看都是随机的微信号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前写过一篇复制微信号并跳转微信的示例,对于固定的一个微信号是可以使用的,但是不能满足多个微信号随机显示并复制跳转的问题。这里补充一下示例。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>随机显示微信号,并复制跳转微信</title>
</head>
<style>
    a:link{text-decoration:none;}/* 指正常的未被访问过的链接*/
    a:visited{text-decoration:none;}/*指已经访问过的链接*/
    a:hover{text-decoration:none;}/*指鼠标在链接*/
    a:active{text-decoration:none;}/* 指正在点的链接*/
    .wx{
        width: 90%;
        margin-left: 5%;
        font-size: 18px;
        color:white;
        text-align: center;
        background-color: #ff7f2e;
        border-radius: 5px;
        line-height: 40px;
    }
    .wx a{
        font-size: 18px;
        color:white;
    }
    /*弹窗*/
    .tan{
        width:80%;
        height:180px;
        background-color:#e8e8e8;
        position:fixed;
        left:10%;
        top:40%;
        display:none;
        z-index: 200;
    }
    .kuang{
        height:120px;
        text-align:center;
        padding: 20px 0;
    }
    .txtcss{
        color:red;
        font-weight:800;
        font-size:20px;
    }
    .copy{
        font-size: 16px;
        float: left;
        background-color: #5AD700;
        padding: 5px 20px;
        margin-left: 25%;color: black;
        border-radius: 5px;
    }
    .copy a{color: white;}
    .guanbi{
        width:20px;
        height:20px;
        color:red;
        margin-top:-130px;
    }
</style>

<body>
    <div class="row">
        张三的微信号是<span onclick="showwx()" class="copy_content txtcss"></span>,(点击微信号复制)
    </div>
    <div class="row">
        张三的微信号是<span onclick="showwx()" class="copy_content txtcss"></span>,(点击微信号复制)
    </div>
    <div class="row">
        <div class="wx" onclick="showwx()">添加微信了解更多</div>
    </div>

    <!--弹窗-->
    <div id='myshow' class="tan">
        <div class="kuang">
            咨询学费<br/>
            了解更多<br/>
            请添加微信:<br/>
            <span id="copy_content" class="copy_content txtcss"></span>
        </div>
        <div style="height: 10px;"></div>
        <p onclick="copywx()" class="copy"><a href="weixin://">复制并跳转微信</a></p>
        <div onclick="closeshow()" class="closeshow pull-right guanbi">x</div>
    </div>

    <script>
        /*点击复制微信号*/
        function copywx(){
            const range = document.createRange();
            range.selectNode(document.getElementById('copy_content'));
            const selection = window.getSelection();
            if(selection.rangeCount > 0) selection.removeAllRanges();
            selection.addRange(range);
            document.execCommand('copy');
            alert("复制成功!");
        }
        /*点击出现弹框*/
        function showwx(){
            var ss=document.getElementById('myshow');
            ss.style.display='block';
        }
        /*关闭弹窗*/
        function closeshow(){
            var ss=document.getElementById('myshow');
            ss.style.display='none';
        }
        /*微信号数组*/
        var arr_wx=['微信1','微信2','微信3'];
        var index=0;
        //进入页面每隔一定时间随机显示微信号
        function change(){
            var copy_content=document.getElementsByClassName('copy_content');
            index=Math.floor(Math.random()*arr_wx.length);
            for(var i=0;i<copy_content.length;i++){
                copy_content[i].innerText=arr_wx[index];
            }
        }
        change();
        setInterval('change()',1000*60);
    </script>


</body>
</html>

效果如下,样式可以自己调整
在这里插入图片描述

Jq(jQuery)是一个流行的JavaScript库,它简化了DOM操作和事件处理。要在网页上实现点击按钮后复制微信微信中打开加好友页面,你可以结合HTML、CSS和JavaScript/jQuery来完成这个功能。以下是简单的步骤: 1. **HTML结构**: ```html <button id="copyBtn">复制微信号</button> <input type="hidden" id="wxNumber" value="your_wechat_username"> <!-- 这里的div是模拟的,真实应用中可能需要通过AJAX获取真实的微信号 --> <div class="wechat-id hidden" data-clipboard-text="#wxNumber">微信号</div> ``` 2. **CSS**: ```css .copy-btn { display: none; } .wx-id { /* 隐藏元素防止直接显示 */ white-space: nowrap; } ``` 3. **jQuery + Clipboard.js (用于复制文本)**: ```javascript $(document).ready(function() { // 点击按钮时触发复制 $('#copyBtn').click(function() { var wxNumber = $('#wxNumber').val(); new ClipboardJS('.wechat-id'); // 初始化剪贴板插件 $('.wechat-id').attr('data-clipboard-text', wxNumber); // 设置复制内容 $('.wechat-id').trigger('mouseleave'); // 触发离开事件,防止延迟执行 setTimeout(function() { // 异步复制以避免干扰 $('.wechat-id').tooltip({ title: '微信号已复制', placement: 'bottom' }).tooltip('show'); }, 50); }); }); ``` 4. **当用户点击“微信号”区域时,链接会自动跳转微信添加好友页**: ```javascript // 使用正则匹配到微信号前缀 var matchWeChat = function(e) { if (e.target.tagName === 'INPUT') return; var target = $(e.target); if (!target.is('.wechat-id')) return; var wxLink = 'weixin://add/?__biz=' + encodeURIComponent(target.data('clipboard-text')); window.location.href = wxLink; // 打开微信加好友页面 }; // 监听鼠标移入和移出事件 $('.wechat-id').on('mouseenter mouseleave', matchWeChat); // 当鼠标离开后移除监听,以释放资源 $('#copyBtn').on('mouseleave', function() { $('.wechat-id').off('mouseenter mouseleave', matchWeChat); }); ``` 注意:以上示例假设你已经包含了jQuery和Clipboard.js库,且在实际项目中,你需要确保在服务器端获取正确的微信号值,在前端动态插入到页面中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值