获取元素标签
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
window.onload = function (){
var oP =document.getElementById('op');
alert(oP);
};
</script>
</head>
<body>
<p id='op'>哈哈,我是p标签</p>
</body>
</html>
标签属性的获取与设置
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.butnstyle{
background-color: chartreuse;
}
</style>
<script>
window.onload =function(){
var oBtn = document.getElementById("btn1");
alert(oBtn.type);
alert(oBtn.value)
oBtn.name = 'username';
oBtn.className ="butnstyle";
};
</script>
</head>
<body>
<input type="button" value="按钮" id="btn1">
</body>
</html>
设置标签包裹的内容-inner html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
window.onload =function(){
var dIv1 =document.getElementById("div1")
alert(dIv1.innerHTML);
dIv1.innerHTML ="<a herf ='http://www.baidu.com'>baidu</a>"
};
</script>
</head>
<body>
<div id="div1">这是一个div的标签</div>
</body>
</html>
设置标签样式及其属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./main.js"></script>
<script>
$(function(){
var $p1 = $("p");
alert($p1);
var $input1 = $("#pwd1");
alert($input1);
var $name1 = $input1.prop("name");
alert($name1);
$input1.prop({'value':'shiwen159258','class':'pwd'});
});
</script>
<style>
.pwd{
background-color: chocolate;
}
</style>
</head>
<body>
<p>这是一个P标签</p>
<input type="password" name="password" id="pwd1">
</body>
</html>