Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用JS将页面中的每个字母设置为随机颜色。为什么在页面的每个打开标记之前总是有一组额外的span标记?_Javascript_Algorithm - Fatal编程技术网

Javascript 使用JS将页面中的每个字母设置为随机颜色。为什么在页面的每个打开标记之前总是有一组额外的span标记?

Javascript 使用JS将页面中的每个字母设置为随机颜色。为什么在页面的每个打开标记之前总是有一组额外的span标记?,javascript,algorithm,Javascript,Algorithm,JS var part_of_doc = false; //a character has been found that is not part of the visible document var body; //will be used to generate random rgb function randint(max, min) { return Math.floor((Math.random() * (max - min)) + min); } //generate

JS

var part_of_doc = false;  //a character has been found that is not part of the visible document
var body;

//will be used to generate random rgb
function randint(max, min) {
    return Math.floor((Math.random() * (max - min)) + min);
}
//generate a random rgb(x,y,z) string
function randRGB(){
    var color = "rgb(";
    for (var i = 0; i < 3; i++) {
        color += randint(0, 255).toString() + ",";
    }
    color = color.substring(0, color.length - 1);  //formatting the rgb to
    color += ')';  //be a proper rgb value

    return color;
}

//returns a character surrounded by a set of spans with a random background-color rgb value 
function spanify(char){
    var open_span = '<span style="color:' + randRGB() + '">';
    return open_span + char + '</span>';  //return the opening tag of a random color rgb tag, character, and a closing span
}

body = document.body.innerHTML;
for (var i=body.length - 1; i>=0; i--){
    body = document.body.innerHTML;  //update body
    console.log(body[i], i);

    if (body[i] == ">") {  //the upcoming characters are part of an html tag and should be ignored
        part_of_doc = false;
        console.log(false);

    } else if (body[i] == "<") {  //the following characters may be part of the visible page and should not be ignored
        part_of_doc = true;
        console.log(true);

    } else if (part_of_doc == true && body[i] != " ") {  //found a character that is part of the visible page
        console.log("changing?");
        document.body.innerHTML = body.substring(0, i) + spanify(body[i]) + body.substring(i + 1, body.length);  //insert a span with a random color to the page surrounding a letter of the page
        console.log(document.body.innerHTML);
    }
}
var-part\u of\u-doc=false//已找到不属于可见文档的字符
变异体;
//将用于生成随机rgb
函数randint(最大值、最小值){
返回Math.floor((Math.random()*(max-min))+min);
}
//生成随机rgb(x,y,z)字符串
函数randRGB(){
var color=“rgb(”;
对于(变量i=0;i<3;i++){
color+=randint(0255).toString()+“,”;
}
color=color.substring(0,color.length-1);//将rgb格式化为
颜色+=')';//是正确的rgb值
返回颜色;
}
//返回由一组具有随机背景色rgb值的跨距包围的字符
函数spanify(char){
var open_span='';
返回open_span+char+'';//返回随机颜色rgb标记的开始标记、字符和结束标记
}
body=document.body.innerHTML;
对于(var i=body.length-1;i>=0;i--){
body=document.body.innerHTML;//更新body
console.log(body[i],i);
如果(body[i]==“>”){//后面的字符是html标记的一部分,应该忽略
_文件的第_部分=假;
console.log(false);

}否则,如果(body[i]==”您正在为换行符着色-
“\n”
以及可能的
“\r”
字符。请检查
body[i]
是否不是其中之一,然后再对其进行着色:

else if (part_of_doc == true && body[i] != " " && body[i] != "\r" && body[i] != "\n")

您正在给换行符上色-
“\n”
以及可能的
“\r”
字符。请检查
正文[i]
是否不在其中,然后再将其变为:

else if (part_of_doc == true && body[i] != " " && body[i] != "\r" && body[i] != "\n")

谢谢!成功了。谢谢!成功了。