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()
如何检测浏览器的类型
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">
<script>
window.callback = function(data){
//这里我们跨域得到信息
console.log(data)
}
</script>
原文:https://www.cnblogs.com/DCL1314/p/10393128.html