当前位置:网站首页>Add random attributes to the Li class array objects and sort them
Add random attributes to the Li class array objects and sort them
2022-04-23 08:25:00 【beinlife】
<html>
<head>
<title> Modify the attributes of an element </title>
<meta charset="utf-8" />
<script >
window.οnlοad=function(){
// obtain ul All under li
var lis=document.querySelectorAll("ul li");
var nums=[];
// Generate random numbers repeatedly : It has to be with lis The numbers are the same
while(nums.length < lis.length) {
var n=parseInt(Math.random()*lis.length+1);
if(nums.indexOf(n)==-1){
nums.push(n);
}
}
/* Traverse the random number of production , For each li Add features */
for (var i = 0; i < nums.length; i++) {
lis[i].setAttribute("data-j",nums[i]);
}
/*
problem 1:lis Is a class array object , No, sort Method
solve 1: Convert class array objects into standard array objects
problem 2: The comparator */
lis=Array.prototype.slice.call(lis);
lis=lis.sort(function(li1,li2){
var n1=parseInt(li1.getAttribute("data-j"));
var n2=parseInt(li2.getAttribute("data-j"));
return n1-n2;
});
for (var i = 0; i < nums.length; i++) {
console.log(lis[i].getAttribute("data-j")+lis[i].innerHTML);
}
}
</script>
</head><body>
<!--1. Using custom attributes to achieve Yaohao ranking -->
<ul>
<li>Eric</li>
<li>Scott</li>
<li>Jerry</li>
<li>Tom</li>
<li>Rose</li>
<li>John</li>
<li>Smith</li>
<li>Andy</li>
</ul>
</body>
</html>
版权声明
本文为[beinlife]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230734168899.html
边栏推荐
- Record: JS several methods to delete one or more items in the array
- 记录:js删除数组中某一项或几项的几种方法
- RPC procedure
- Comparison of indoor positioning technology
- word加水印
- 作文以记之 ~ 二叉树的前序遍历
- 怎么读书读论文
- 单点登录 SSO
- There are some problems when using numeric type to query string type fields in MySQL
- Sword finger offer Day24 math (medium)
猜你喜欢

Talk about the basic but not simple stock data

监控智能回放是什么,如何使用智能回放查询录像

Description of the abnormity that the key frame is getting closer and closer in the operation of orb slam

Online yaml to XML tool

LeetCode中等题之旋转函数

One click cleanup of pycharm and jupyter cache files under the project

dried food! Point based: differentiable Poisson solver

ansible自动化运维详解(一)ansible的安装部署、参数使用、清单管理、配置文件参数及用户级ansible操作环境构建

vslam PPT

Idea: export Yapi interface using easyyapi plug-in
随机推荐
作文以记之 ~ 二叉树的前序遍历
Navicat远程连接mysql
Interesting JS code
Comparison of indoor positioning technology
Sword finger offer Day24 math (medium)
[explanation] get ora-12838: cannot read / modify an object after modifying it in parallel
A simple theme of Typecho with beautiful appearance_ Scarfskin source code download
使用JWT生成与解析Token
How to encrypt devices under the interconnection of all things
QFileDialog select multiple files or folders
Input / output system
Generate and parse tokens using JWT
LeetCode简单题之三除数
作文以记之 ~ 二叉树的后序遍历
通过实现参数解析器HandlerMethodArgumentResolver接口来自定义注解
JS converts tree structure data into one-dimensional array data
QFileDialog 选择多个文件或文件夹
分布式消息中间件框架选型-数字化架构设计(7)
JS中复制数组
单点登录 SSO