I am VERY new to JavaScript. I have looked at several previously answered questions along this line. My HTML is roughly:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.skills{
list-style:none;
padding: 0;
margin-bottom: 200px;
}
.skills li{
width: 200px;
float: left;
padding: 0;
}
</style>
</head>
<body>
<h2>Skills</h2>
<ul class="skills">
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
<li>e</li>
</ul>
</body>
</html>
I have tried to use:
<script type="text/javascript">
function sortLi()
{
var skills = [getElementsbyTagName("li")];
skills.sort();
var x=document.getElementsByTagName("li");
x.innerHTML=skills;
}
</script>
and by swapping .getElementsbyTagName with .getElementsbyClass("skills") to no avail. I know that I am doing something wrong but just can't put my finger on it.
Check out this solution (here is a link at jsfiddle.)
var mylist = $('.skills');
var listitems = mylist.children('li').get();
listitems.sort(function(a, b) {
return $(a).text().toUpperCase().localeCompare($(b).text().toUpperCase());
})
$.each(listitems, function(idx, itm) {
mylist.append(itm);
});
This uses jQuery and is almost wholly lifted from another Stackoverflow answer.
Basically you have some syntax errors, you don't call your code, etc. Take a look at how the working solution I've linked to at jsfiddle functions and see if that helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.