有没有任何字符串方法,能让我把一串数字,转换成一串数字和字母

Are there any string methods, that will let me convert a string of numbers, to string of numbers and letters?

本文关键字:一串 数字 转换 任何 方法 有没有 字符串      更新时间:2023-09-26

是否有任何字符串方法,可以让我将一串数字转换为一串数字和字母?

我正在尝试创建一个带有按钮的页面。一旦按钮被点击,它将改变body元素的背景颜色。但是,无论生成的数字多么随机,都不会在字符串中引入字母a-fa-f。有什么字符串方法能帮上忙吗? jsfiddle .

JavaScript


(function(){ 
    var body;
    var button;
    body = document.querySelector('body');
    button = document.querySelector('span');
    function changeColor() {
        body.style.background = "#" + String(Math.random()).substring(2, 8);    
    }
    button.onclick = changeColor;
})();

toString(16)将创建一个颜色十六进制,这是最常用的随机颜色代码

body.style.background = '#'+Math.floor(Math.random()*16777215).toString(16);

小提琴