Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.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
如何对名称进行排序(Li元素-Javascript)_Javascript - Fatal编程技术网

如何对名称进行排序(Li元素-Javascript)

如何对名称进行排序(Li元素-Javascript),javascript,Javascript,有人能帮我吗 window.onload = init; function init(){ var button = document.getElementById("Submit"); //ask the document to return the element with id commentSubmission button.onclick=KeyPress; //when the button is clicked, the function validate i

有人能帮我吗

window.onload = init; 


function init(){
    var button = document.getElementById("Submit"); //ask the document to return the element with id commentSubmission
    button.onclick=KeyPress; //when the button is clicked, the function validate is called
}


function KeyPress() {

    var visitorname = document.getElementById("name");
    var visitorcomment = document.getElementById("comment");
    var name = visitorname.value; //get the value of the object
    var comment = visitorcomment.value;
    var flag=0; //use to validate if the textboxes contain data

    if (name==null || name=="") {
        alert("Please Enter Name, Name Field is blank"); // This displays an error in terms of alert if name has not been input
        flag = 1; // Flag increments to one thus final if statement cannot be executed
    }

    if (comment==null || comment=="") {
        alert("Please Enter Comment, Comment field is blank");
        flag = 1;
    }

    if (flag==0){
        var finalComment = (name +" : " + comment); //concatenate the variable to form the output
        var li = document.createElement("li");
        li.innerHTML = finalComment; //inserting the content of the variable finalcomment in the object li
        var ul = document.getElementById("**listofcomments"**); //using variable ul as an object
        ul.appendChild(li); //including the li object in the ul object
    }
}
有两个输入字段名和注释 通过使用getElementById,在单击submit按钮时,我允许多个注释

有人能帮我整理一下这些评论吗

注释与对象注释列表关联

我需要帮助对它进行排序,如果同一个人评论两次,
此人的姓名将显示在彼此下方

如果将注释保存在对象数组中,则可以使用函数按属性对数组进行排序

在这个演示中,我使用了一个compare函数作为sort函数中的参数,通过
name
返回排序后的注释数组

参数:

比较函数:指定定义排序顺序的函数。如果省略,则根据每个字符的Unicode对数组进行排序 代码点值,根据每个元素的字符串转换

您可以看到这个演示是如何工作的

希望这有帮助

window.onload=init;
函数init(){
var按钮=document.getElementById(“提交”);
button.onclick=按键;
}
var注释=[];//评论数组。
功能按键(){
变量名称、注释;
名称=document.getElementById(“名称”);
comment=document.getElementById(“comment”);
//验证字段并在控件为空时聚焦控件。
if(name.value.length==0){
警报(“请输入名称,名称字段为空”);
name.focus();
返回;
}
if(comment.value.length==0){
警报(“请输入注释,注释字段为空”);
comment.focus();
返回;
}
//在注释数组中添加注释。注释对象具有名称和注释属性。
评论。推({
name:name.value,
comment:comment.value
});
sortComments();//按名称对注释排序。
}
函数sortComments(){
sort(函数(a,b){//Compare函数。
返回a.name>b.name;
});
打印评论();
}
函数printComments(){
变量i,len=comments.length,
注释={},
list=“
    ”; 对于(i=0;i”;//为comments数组中的每个排序注释创建li标记。 list+=comment.name; 列表+=“:”; list+=comment.comment; 列表+=“”; } 列表+=“
”; document.getElementById(“注释列表”).innerHTML=list; }
名称:

评论:

提交


    您能提供一个工作的JSFIDLE吗?您不使用Jquery的任何原因?这样的事情会让生活简单很多。关于Jquery的一个类似问题,请参见以下内容:非常感谢,不客气。不要忘记接受此答案,以帮助其他有类似问题的用户。:)