var div1 = document.getElementById('div1');//元素 var divList = document.getElementsByTagName('div');//集合 console.log(divList.length); console.log(divList[0]); var containerList = document.getElementsByClassName('.container'); var pList = document.querySelectorAll('p');//集合
var pList = document.querySelectorAll('p'); var p = pList[0]; console.log(p.style.width);//獲取樣式 p.style.width = '100px';//修改樣式 console.log(p.className);//獲取class p.className = 'p1';//修改class //獲取nodeName和nodeType console.log(p.nodeName); console.log(p.nodeType)
var pList = document.querySelectorAll('p'); var p = pList[0]; p.getAttribute('date-name'); p.setAttribute('date-name','imooc'); p.getAttribute('style'); p.setAttribute('style','font-size:30px')
var div1 = document.getELementById('div1'); //添加新節點 var p1 = document.createElement('p'); p1.innerHTML = 'this is p1'; div1.appendChild(p1);//添加新建立的元素 //移除已有節點 var p2 = document.getElementById('p2'); div1.appendChild(p2);
var div1 = document.getELementById('div1'); var parent = div1.parentElement;//父元素 var child = div1.childNodes;//子元素 div1.removeChild(child[0]);//刪除節點
//navigator var ua = navigator.userAgent; var isChrome = ua.indexOf('chrome'); console.log(isChrome) //screen console.log(screen.width); console.log(screen.height);
location.protocal //'http' 'https' location.host //集合 location.pathname //'/learn/199'路徑 location.search //?參數 location.hash //# //history history.back() history.forward()
如何檢測瀏覽器的類型html
var ua = navigator.userAgent; var isChrome = ua.indexOf('chrome'); console.log(isChrome)
var btn = document.getElementById('btn1'); btn.addEventListener('click',function(event){ console.log('clicked'); }) function bindEvent(elem,type,fn){ elem.addEventListener(type,fn); } var a = document.getElementById('link1'); bindEvent(a,'click',function(e){ e.preventDefault();//阻止默認行爲 alert('clicked'); })
<div id="div1"> <p id="p1">激活</p> <p id="p2">激活</p> <p id="p3">激活</p> <p id="p4">激活</p> </div> <div id="div2"> <p id="p5">取消</p> <p id="p6">取消</p> </div> var p1 = document.getElementById('p1'); var body = document.body; function bindEvent(elem,type,fn){ elem.addEventListener(type,fn); } bindEvent(p1,'click',function(e){ e.stopPropagation()//阻止冒泡 alert('激活'); }) bindEvent(body,'click',function(e){ alert('取消') })
<div id="div1"> <a href="#">a1</a> <a href="#">a2</a> <!-- 會隨時新增更多a標籤 --> </div> <script> var div1 = document.getElementById('div1'); div1.addEventListener('click',function(e){ var target = e.target; if(target.nodeName === 'A'){ alert(target.innerHTML) } }) </script>
<div id="div1"> <a href="#">a1</a> <a href="#">a2</a> <!-- 會隨時新增更多a標籤 --> </div> <script> function bindEvent(elem,type,selector,fn){ if(fn == null){ fn = selector; selector = null; } elem.addEventListener(type,function(e){ var target; if(selector){ target = e.target; if(target.matches(selector)){ fn.call(target,e) } }else { fn(e); } }) } //使用代理 var div1 = document.getElementById('div1'); bindEvent(div1,'click','a',function(e){ e.preventDefault(); console.log(this.innerHTML) }) </script>
var xhr = XMLHttpRequest; xhr.open('GET','api',false); xhr.onreadystatechange = function(){ //這裏的函數異步執行,可參考以前js基礎的異步模塊 if(xhr.readyState == 4){ if(xhr.status == 200){ alert(xhr.responseText) } } } xhr.send(null); //IE兼容問題; //IE低版本使用ActiveXObject和w3c標準不同
<img src="xxx" />
<link href="xxx" />
<script src="xxx" />
<img>
用於打點統計,統計網站多是其餘域<link>``<script>
可使用CND、CND的也是其餘域<script>
能夠用JSONP同理於<script src="http:www.baidu.com/api.js">
node
<script> window.callback = function(data){ //這裏咱們跨域獲得信息 console.log(data) } </script>