当前位置:网站首页>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
边栏推荐
- The simple problem of leetcode is to calculate the numerical sum of strings
- Introduction to protobuf
- 分组背包呀
- 欧圣电气深交所上市:市值52亿 陆为东父女为美国籍
- form中enctype属性
- Input / output system
- 396. Rotate Function
- freertos学习02-队列 stream buffer message buffer
- An idea plug-in that doesn't work, but can install X
- Shell script advanced
猜你喜欢
A simple theme of Typecho with beautiful appearance_ Scarfskin source code download
Transformer-XL: Attentive Language ModelsBeyond a Fixed-Length Context 论文总结
Overview of bus structure
Ansible Automation Operation and Maintenance details (ⅰ) Installation and Deployment, Parameter use, list Management, Profile Parameters and user level ansible operating environment Construction
SYS_CONNECT_BY_PATH(column,'char') 结合 start with ... connect by prior
QT reading and writing XML files
LeetCode简单题之计算字符串的数字和
Search the complete navigation program source code
RPC procedure
LeetCode-199-二叉树的右视图
随机推荐
QFileDialog 选择多个文件或文件夹
分组背包呀
JS中复制数组
rust 使用tokio的Notify 和timeout实现类似可超时条件变量的效果
AQS & ReentrantLock 实现原理
总线结构概述
Shell脚本进阶
常用正则表达式
【深度好文】Flink SQL流批⼀体化技术详解(一)
监控智能回放是什么,如何使用智能回放查询录像
输入/输出系统
Online app resource download website source code
Goland 调试go使用-大白记录
PyQt5开发之QTableWidget表头自定义与美化(附源代码下载)
Let the earth have less "carbon" and rest on the road
clang 如何产生汇编文件
作文以记之 ~ 二叉树的后序遍历
Detailed description of self feeling of auricular point weight loss 0422
dried food! Point based: differentiable Poisson solver
2022.4.11-4.17 AI industry weekly (issue 93): the dilemma of AI industry