Javascript 获取要作为数组操作的跨HTML值

Javascript 获取要作为数组操作的跨HTML值,javascript,html,arrays,getelementbyid,Javascript,Html,Arrays,Getelementbyid,事实上,我有一个HTML学生表格和笔记,每次你点击按钮谁重定向到anadirNota。从输入TXTNOA中获取值,并将其添加到span SPANNOAS中。当您单击anadirAlumno按钮时,将表单的值添加到表的新行,但使用spanNotas作为数组来计算方法obtenerMedia。 我的问题是:如何让de spanNotas元素作为int数组进行操作 例如: 名字:艾尔 阿佩利多:巫统 注:[2、6、8] 当我单击de añadir alumno按钮时,会插入以下行: Nombre |

事实上,我有一个HTML学生表格和笔记,每次你点击按钮谁重定向到anadirNota。从输入TXTNOA中获取值,并将其添加到span SPANNOAS中。当您单击anadirAlumno按钮时,将表单的值添加到表的新行,但使用spanNotas作为数组来计算方法obtenerMedia。 我的问题是:如何让de spanNotas元素作为int数组进行操作

例如:

名字:艾尔 阿佩利多:巫统 注:[2、6、8]

当我单击de añadir alumno按钮时,会插入以下行:

Nombre | Apellidos | Media
Al     | Umno      | 5,33
HTML代码:

 <body>
        Nombre:<input id="txtNombre" type="text" />
        &nbsp;&nbsp;&nbsp;
        Apellido:<input id="txtApellido" type="text" />
        &nbsp;&nbsp;&nbsp;
        Notas:&nbsp;[<span id="spanNotas"></span>]
        <br/>
        Nota:<input id="txtNota" type="text" />
        &nbsp;
        <input type="button" value="Añadir nota" onclick="anadirNota()"/>
        <br/><br/>
        <input type="button" value="Añadir alumno" />
        <br/><br/>
        <table id="tablaAlumnos">
            <tr>
                <th>Nombre</th>
                <th>Apellido</th>
                <th>Media</th>
            </tr>
        </table>
    </body>
Javascript代码:

正如lolka_bolka所说:listaNota包含一个字符串,例如2、4、5,所以通过在每个位置拆分,使其成为一个数组,。.map函数将所有部分转换为数字。在最后一步,数组被转换回一个带有.join的字符串

问题是,您的listaNotas是一个字符串。定义一个全局变量什么是数组;而是使用推送功能。
    function anadirNota() {
        var nota = parseInt(document.getElementById("txtNota").value);
        console.log(nota);
        if(!isNaN(nota)) {

            var listaNotas = document.getElementById("spanNotas").innerHTML;
            console.log(listaNotas);
            listaNotas += nota;
            console.log(listaNotas);
            document.getElementById("spanNotas").innerHTML=listaNotas+", ";
        } else {
            alert("El valor del campo nota no es válido")
        }
    }

    function anadirAlumno() {

        var nombre = document.getElementById("txtNombre").value;
        var apellido = document.getElementById("txtApellido").value;
        var notas = document.getElementById("spanNotas").innerHTML;

        if(nombre != "" || apellido != "" || notas.isEmpty ) {

            var Alumno1 = new Alumno(nombre, apellido, notas);
            var notaMedia = Alumno1.obtenerMedia();

            console.log(Alumno1);

            var table = document.getElementById("tablaAlumnos");
            fila = table.insertRow(table.length);
            fila.insertCell (0).innerHTML=nombre;
            fila.insertCell (1).innerHTML=apellido;
            fila.insertCell (2).innerHTML=notaMedia;
        } else {
            alert("Los datos del alumno no son correctos")
        }
    }

    var Alumno = function(nombre, apellido, notas)
    {
        this.nombre = nombre;
        this.apellido = apellido;
        this.notas = notas;

        this.obtenerMedia = function() {
            var media = 0;

            for(i = 0; i < this.notas.length; i++)
                media += this.notas[i];

            media /= this.notas.length;
            return media.toFixed(2);
        };
    };
</script>
if(!isNaN(nota)) { 
    var listaNotas = document.getElementById("spanNotas").innerHTML;
        console.log(listaNotas);
    var notaArray = listaNotas.split(',').map(function(n) {return parseFloat(n);})
        console.log(notaArray);
    notaArray.push(nota);
        console.log(notaArray);
    document.getElementById("spanNotas").innerHTML = notaArray.join(', ');
}