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';
注意
通过样式属性设置宽高、位置的属性类型是字符串,需要加上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="开/关灯" id="btn"/>
<script src="common.js"></script>
<script>
my$("btn").onclick = function () {
//获取body标签
document.body.className = document.body.className != "cls" ? "cls" : "";
};
</script>
</body>
</html>
联系客服