用 <br> 替换 ,

3

我学习编程课程以来一直在浏览stackoverflow,发现这是一个非常棒的社区和获取知识的好地方。

目前我卡在了一段JavaScript函数上,我正在尝试进行清理。

我需要将名字输入到一个数组中,然后当我运行“开始”函数时,它会显示姓名数量并在新的一行上显示每个姓名。

我已经成功让它工作了,但是每行开头都有一个“,”字符。我尝试了各种方法来解决它(使用replace和split + join),但至今没有成功。

var arrName = [];
var custName;

function start(){
    var totalName = 0;
    var count = 0;

    document.getElementById("output").innerHTML = " ";

    while(count < arrName.length){
        totalName++;
        count++;
    };
    document.getElementById("output").innerHTML = "The total names in the array are: " + totalName + "<br />" + arrName;
}

function addName(){
    custName = document.getElementById("custname").value;

    if(!custName){
        alert("Empty Name!");
    }
    else{
        arrName.push(custName + "<br>");
        return custName;}
}

2
document.getElementById("output").innerHTML = "The total names in the array are: " + totalName + "<br />" + arrName; 中,你将一个数组放入了 DOM (arrName)。由于数组被字符串化,, 被添加在每个元素之间。将 arrName 改为 arrName.join("<br/>"),然后你可以从 arrName.push(custName + "<br>"); 中删除 <br> - eithed
2个回答

0

当数组被控制台记录或直接插入到DOM中时,它将表示为item1,item2,item3,item4

因此,简单地使用arrName.join("<br />")

此外,您的while循环可以简单地替换为

totalName = arrName.length; count = arrName.length


0
原因很可能是您正在尝试显示一个数组arrName,使用了以下代码:document.getElementById("output").innerHTML = "The total names in the array are: " + totalName + "<br />" + arrName; 这是我的建议:
        var arrName = [];
        var custName;

        function start(){
            var totalName = 0;
            var count   = 0;

            document.getElementById("output").innerHTML = " ";

            while(count < arrName.length){
                totalName++;
                count++;
            }
            var strOutput   = "The total names in the array are: ";
            strOutput      += totalName + "<br />" +  arrName.join("<br />");
            document.getElementById("output").innerHTML = " ";
            document.getElementById("output").innerHTML = strOutput;
        }

        function addName(){
            custName = document.getElementById("custname").value;

            if(!custName){
                alert("Empty Name!");
            }
            else{
                arrName.push(custName);
                return custName;}
        }

由于不需要 WHILE 循环,您可以这样跳过它:
        var arrName = [];
        var custName;

        function start(){
            var totalName   = arrName.length;
            var strOutput   = "The total names in the array are: ";
            strOutput      += totalName + "<br />" +  arrName.join("<br />");
            document.getElementById("output").innerHTML = " ";
            document.getElementById("output").innerHTML = strOutput;
        }

        function addName(){
            custName = document.getElementById("custname").value;

            if(!custName){
                alert("Empty Name!");
            }
            else{
                arrName.push(custName);
                return custName;
            }
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接