href、title、id、src、className
var link = document.getElementById('link');
console.log(link.href);
console.log(link.title);
var pic = document.getElementById('pic');
console.log(pic.src);
案例:
? 點擊按鈕,切換img標簽里的圖片
? 點擊按鈕顯示隱藏div
var box = document.getElementById('box');
box.innerHTML = '我是文本<p>我會生成為標簽</p>';
console.log(box.innerHTML);
box.innerText = '我是文本<p>我不會生成為標簽</p>';
console.log(box.innerText);
""
''
&&
<< //less than 小于
>> // greater than 大于
空格
?©
var box = document.getElementById('box');
box.style.width = '100px';
box.style.height = '100px';
box.style.backgroundColor = 'red';
注意
通過樣式屬性設(shè)置寬高、位置的屬性類型是字符串,需要加上px
var box = document.getElementById('box');
box.className = 'clearfix';
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>開燈</title>
<style>
.cls {
background-color: black;
}
</style>
</head>
<body id="bd">
<input type="button" value="開/關(guān)燈" id="btn"/>
<script src="common.js"></script>
<script>
my$("btn").onclick = function () {
//獲取body標簽
document.body.className = document.body.className != "cls" ? "cls" : "";
};
</script>
</body>
</html>
聯(lián)系客服