day 0701

编程入门 行业动态 更新时间:2024-10-28 19:25:58

<a href=https://www.elefans.com/category/jswz/34/1768188.html style=day 0701"/>

day 0701

location.hash	//如果URL包含#,返回该符号之后的内容,如#anchor1 
/* 
hash  一般用于锚点或者路由
当改变hash会触发onhashchange事件
可以通过hash改变产生历史记录,也可以通过历史回退前进切换hash
可以通过hash不同设置同页面中不同的位置,以及同页面中不同的内容
*/
location.host	//IP+Port
location.hostname	//IP
location.href //当前载入页面的完整URL
location.port //URL声明的请求的端口
location.reload(true | false); //重新载入当前页面,为false时从浏览器缓存中重载,为true时从服务器端重载,默认为false
//获取浏览器定位信息
navigator.geolocation.getCurrentPosition(function(geo){
console.log(geo.coords);
},function(err){
console.log(err);
})
//谷歌浏览器会报错,原因:默认使用谷歌地图,信息安全原因

SSR与CSR的了解链接:点我

文档碎片容器

createDocumentFragment()

DocumentFragment,文档片段接口,一个没有父对象的最小文档对象。它被作为一个轻量版的 Document 使用,就像标准的document一样,存储由节点(nodes)组成的文档结构。与document相比,最大的区别是DocumentFragment 不是真实 DOM 树的一部分,它的变化不会触发 DOM 树的重新渲染,且不会导致性能等问题。

虚拟DOM 复制DOM(不复制属性等),解决元素属性更改页面就重绘的问题

了解1 了解2

案例:

//历史记录var arr=[["苹果","香蕉","西瓜","桃子","杏","榴莲","葡萄","芒果","荔枝","菠萝","火龙果","橘子","橙子"],["番茄","白菜","菠菜","茄子","土豆","豆角","黄瓜","油菜","油麦菜","花白","韭菜","西蓝花"],["小米","大米","燕麦","黑米","薏仁","绿豆","红豆","黄豆","花生油","菜籽油","玉米油","橄榄油","调和油"],["鸡","鸭","鱼","牛肉","羊肉","狗肉","驴肉","蛇肉","排骨","肋条","牛腱","眼肉","五花肉","鸡爪","鸡脖"],["虾爬子","带鱼","鲅鱼","平鱼","生蚝","石斑鱼","多宝鱼","章鱼","澳龙","青龙","鲍鱼","海胆","帝王蟹","海参"]]var list=["水果","蔬菜","粮油","禽肉","海鲜"]var lis,div,prev;init();function init(){lis=Array.from(document.getElementsByTagName("li"));div=document.getElementById("div1");lis.forEach(function(item){item.onclick=clickHandler;});var index=history.state;if(index===null)index=0;history.replaceState(index,lis[index].innerHTML)changePrev(lis[index]);window.onpopstate=popstateHandler;}function clickHandler(){changePrev(this);history.pushState(list.indexOf(this.innerHTML),this.innerHTML);}function setData(elem){var data=arr[list.indexOf(elem.innerHTML)];div.innerHTML=data.reduce(function(value,item){return value+item+"&emsp;&emsp;&emsp;"},"");}function changePrev(elem){if(prev){prev.style.backgroundColor="orange"prev.style.boxShadow="none";}prev=elem;prev.style.backgroundColor="rgb(255, 217, 0)"prev.style.boxShadow="0px 0px 3px 3px rgb(255, 253, 148) inset";setData(prev);}function popstateHandler(){if(history.state===null) return;changePrev(lis[history.state]);}
//历史记录hashvar arr=[["苹果","香蕉","西瓜","桃子","杏","榴莲","葡萄","芒果","荔枝","菠萝","火龙果","橘子","橙子"],["番茄","白菜","菠菜","茄子","土豆","豆角","黄瓜","油菜","油麦菜","花白","韭菜","西蓝花"],["小米","大米","燕麦","黑米","薏仁","绿豆","红豆","黄豆","花生油","菜籽油","玉米油","橄榄油","调和油"],["鸡","鸭","鱼","牛肉","羊肉","狗肉","驴肉","蛇肉","排骨","肋条","牛腱","眼肉","五花肉","鸡爪","鸡脖"],["虾爬子","带鱼","鲅鱼","平鱼","生蚝","石斑鱼","多宝鱼","章鱼","澳龙","青龙","鲍鱼","海胆","帝王蟹","海参"]]var list=["水果","蔬菜","粮油","禽肉","海鲜"]var lis,div,prev;init();function init(){lis=Array.from(document.getElementsByTagName("li"));div=document.getElementById("div1");lis.forEach(function(item){item.onclick=clickHandler;});var index=~~(location.hash.slice(1));history.replaceState(index,lis[index].innerHTML,"#"+index);changePrev(lis[index]);window.onhashchange=hashchangeHanlder;}function clickHandler(){changePrev(this);var index=list.indexOf(this.innerHTML);history.pushState(index,this.innerHTML,"#"+index);}function setData(elem){var data=arr[list.indexOf(elem.innerHTML)];div.innerHTML=data.reduce(function(value,item){return value+item+"&emsp;&emsp;&emsp;"},"");}function changePrev(elem){if(prev){prev.style.backgroundColor="orange"prev.style.boxShadow="none";}prev=elem;prev.style.backgroundColor="rgb(255, 217, 0)"prev.style.boxShadow="0px 0px 3px 3px rgb(255, 253, 148) inset";setData(prev);}function hashchangeHanlder(){var index=~~(location.hash.slice(1));changePrev(lis[index]);}

作业(后补):

1.获取当前浏览器版本和型号的函数

2.document.body获取类似于"我的产品"标签内容10个

更多推荐

day 0701

本文发布于:2023-07-03 12:20:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1006330.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:day

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!