-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[js] 第28天 解释下这段代码的意思! #100
Comments
作用
解析
手写简版
看蒙了,别问我怎么知道的,百度的!! 参考:
|
用数组的foEach方法遍历 dom类数组对象 ,将对象dom节点加上随机颜色的轮廓。 |
// $$('*') 为获取所有 dom 元素,返回数组
[].forEach.call($$("*"), function(a) {
// forEach 的回调函数,这里的 a 是数组中每个 dom 元素,不是 a 标签
a.style.outline =
// ~~是取整 1<<24 是位运算 结果为 16777216
// 之后的 toString(16) 为进行 16 进制的转换 即颜色
"1px solid #" + (~~(Math.random() * (1 << 24))).toString(16);
}); 因此这段代码的意思为,给页面所有 |
用通配符拿到所有元素 |
1 << 24; // 1 左移 24 位, 值为 2^24
Math.random() * (1 << 24); // 随机生成 (0, 2^24) 之间的数字
~~(Math.random() * (1 << 24)); // 将小数转成32位有符号整数取反再取反, 即舍弃小数值
(~~(Math.random() * (1 << 24))).toString(16); // 将取整后的值转化为16进制, 值范围 [0, 2^24)
$$("*"); // 相当于 document.querySelectorAll("*")
// 代码为所有元素设置一个随机颜色的 outline |
等于这个$$("").forEach(function(a){ a.style.outline="1px solid #"+(~~(Math.random()(1<<24))).toString(16) }) 整个的意思就是,页面上全部元素弄一个1px solid随机颜色的轮廓 |
[].forEach.call() => 调用引用数组的forEach方法 |
mark 一下 |
给网页的所有标签元素加上一个随机颜色的边框 |
第28天 解释下这段代码的意思!
The text was updated successfully, but these errors were encountered: