目的地-Destination 前程似锦、未来可期、寻得良人、共赴白头,祝你也祝我。
博主 目的地-Destination
沪ICP备16003146号-2沪公网安备 31010702004922号萌ICP备20238488号博主 昨天 17:25 在线自豪地使用 Typecho 建站搭配使用 🌻Sunny 主题当前在线 13 人
歌曲封面 未知作品

沪ICP备16003146号-2

沪公网安备 31010702004922号

萌ICP备20238488号

网站已运行 7 年 342 天 0 小时 58 分

Powered by Typecho & Sunny

14 online · 50 ms

Title

Js/JQuery生成不重复的UUID

Chrison

·

烂笔头

·

Article
⚠️ 本文最后更新于2022年12月12日,已经过了680天没有更新,若内容或图片失效,请留言反馈

javascript代码

♾️ text 代码:
function guid () {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0
        var v = c === 'x' ? r : (r & 0x3 | 0x8)
        return v.toString(16)
    })
}

去除横线

♾️ text 代码:
function guidShort () {
    return guid ().replace("-","");
}

显示结果

♾️ text 代码:
UUID完整:4915d2f4-2e83-47d6-bc76-e321caeff812
UUID不含-:f5e53a50fda8-4c8d-a4fb-04821ac932d0

完整Demo

♾️ text 代码:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
    function guid () {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0
        var v = c === 'x' ? r : (r & 0x3 | 0x8)
        return v.toString(16)
    })
}

function guidShort () {
    return guid ().replace("-","");
}

$(function(){ 
    $("#div1").text('UUID完整:'+guid ());
    $("#div2").text('UUID不含-:'+guidShort ());
});

    
</script>
</head>

<body>
    <div id="div1"></div>
    <div id="div2"></div>
</body>
</html>

现在已有 163 次阅读,0 条评论,0 人点赞
Comment:共0条
发表
搜 索 消 息 足 迹
你还不曾留言过..
你还不曾留下足迹..
博主 不再显示
博主