在 for 循环中使用 if 语句 - Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9298097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:12:26  来源:igfitidea点击:

Using if statement inside for loop - Javascript

javascript

提问by Joe

I have a Javascript For Loop...

我有一个 Javascript For 循环......

var CookieName = "Tab,EduTab,EduTab,user";
var tString = CookieName.split(',');

for(i = 0; i < tString.length; i++){

    if (tString[i] == "EduTab") {
        document.write("<b>"+tString[i]+"<b>"); 
    } else {
        document.write(tString[i]);
    }
}

For some reason it is failing to bold the 'EduTab'. It will either bold the entire array CookieName or none at all. Any help would be awesome. Thanks.

出于某种原因,它没有加粗“EduTab”。它会加粗整个数组 CookieName 或根本不加粗。任何帮助都是极好的。谢谢。

回答by Nettogrof

You are missing the /in your closing tab </b>

/在关闭选项卡 中缺少</b>

回答by nwellcome

You aren't closing the <b>tag

你没有关闭<b>标签

document.write("<b>"+tString[i]+"<b>")

should be

应该

document.write("<b>"+tString[i]+"</b>")