案例一:全选等
运用知识点:ondblclick=()| onclick | 寻找标签属性及判断和函数的应用
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAckAAACgCAIAAADo0QAAAAAMk0lEQVR4nO2dz2scyRmG++/Z/2CvwosvBt9MfLL3YtDZ+BQ7lwX76IsYWT8sJIFkSYsTY628QXaIg3dBIcYxycAuGJRgJ9mQZBOSkFyTw7Cz7e6vv67ueae7VfW8PCyt7uquT7XmUalmNJX9jxBCiDpZ3wUQQkiEwa2EEKIPbiWEEH1wKyGE6INbCSFEH9xKCCH64FZCCNEHtxJCiD64lRBC9MGthBCiD24lhBB9cCshhOiDWwkhRB/cSggh+ijdevrTO9AjX716sbe3t9NHHjx48NWrF0MrbJhV1RYG3SBUnxncGg+7u7v/7S+7u7sDLGyYVfmFQTcI1WcGt8bD9vb2f/rL9vb2AAsbZlV+YdANQvWZwa3xsLW19e/+srW1NcDChlmVXxh0g1B9ZnBrPGxubv4rLFmWhV9yGuezubk5e2GNElKYqqosl9oyZiwMukGoPjO4NR42Nzf/GZAsy6b/dRqEtMzHt5jTVyGBVZkHqqrKhTkty5WEjBhu7R2h+szg1njY2Nj4R12yLDOPqxrn4zfe2NhoV9j0yZMDp8dyy9rvYvaqGiV8xJzCoBuE6jODW+NhY2Pj726yLKs9U77ktMnHt1hIVYWD2n5DCpu9qvA0GjHc2jtC9ZnBrfFw//79bysymUbVXppOuAoNqh6bz/3791sUlu80X0ah38JxOXOtyumr9Yg5hUE3CNVnBrfGw/r6+l/dmKYwm4U3nmZ9fb1dYdPHFg4K3ZWblS/Jqwrsq8WIOYVBNwjVZwa3xsPa2tpfwpJlWeDVybHffpK1tbV2heVNFNKpqbB5VGXWYPbVYsScwqAbhOozg1vjYXV19c9hybIs8Ork2G8/yerqarvCpg8vHFR1Wj7vlCesKrCkwBFzCoNuEKrPDG6Nh5WVlW+qY073Jim3NA/KLfNZWVlpXVi+wsLJcsuQb0FY1TcB336LEXMKg24Qqs8Mbo2He/fu/SksWZbV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KACkiVJ8Z3AoAKSJUnxncCgApIlSfGdwKACkiVJ8Z3AoAKSJUn5n/A8DSYmrNOJ3uAAAAAElFTkSuQmCC" alt="" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div>
<input type="button" value="全选" ondblclick="checkAll()" />
<input type="button" value="取消" ondblclick="cancleAll()" />
<input type="button" value="反选" ondblclick="reverseAll()" />
</div>
<table>
<thead>
<tr>
<th>序列号</th>
<th>用户名</th>
<th>年龄</th>
</tr>
</thead>
<tbody id="tb">
<tr>
<td><input class="c1" type="checkbox"/></td>
<td>alex</td>
<td>19</td>
</tr>
<tr>
<td><input class="c1" type="checkbox"/></td>
<td>alex</td>
<td>19</td>
</tr>
<tr>
<td><input class="c1" type="checkbox"/></td>
<td>alex</td>
<td>19</td>
</tr>
<tr>
<td><input class="c1" type="checkbox"/></td>
<td>alex</td>
<td>19</td>
</tr>
</tbody>
</table>
<script>
//全选的话先获取所有的input标签属性,然后全部为true
function checkAll(){
var tb = document.getElementById('tb');//获取tb,也就是身体
var checks = tb.getElementsByClassName('c1');
//[第一个,2,3,4,5]checked = true
for (var i=0;i<checks.length; i++){
var current_check = checks[i];//得到每个input的值并赋一个变量名
current_check.checked = true;
}
}
function cancleAll(){
var tb = document.getElementById('tb');//获取tb,也就是身体
var checks = tb.getElementsByClassName('c1');
//[第一个,2,3,4,5]checked = true
for (var i=0;i<checks.length; i++){
var current_check = checks[i];//得到每个input的值并赋一个变量名
current_check.checked = false;
}
} function reverseAll(){
var tb = document.getElementById('tb');//获取tb,也就是身体
var checks = tb.getElementsByClassName('c1');
//[第一个,2,3,4,5]checked = true
for (var i=0;i<checks.length; i++){
var current_check = checks[i];//得到每个input的值并赋一个变量名
if(current_check.checked <!--这个地方如果为true-->){
current_check.checked = false;
}else{
current_check.checked = true;
}
}
} </script> </body>
</html>
代码及讲解
案例二:单选
要求只能选一个,并且用js可以改变其选项
运用知识点:getElementsByTagName | checked | 互斥
aaarticlea/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LsseZCvop0L9LW7esbgFBfP+ON+M+hrd/WMAV28GNBrJ6vPGOROZ804Tv1A+d/21wO9dggZA94XLwb02iFkDNSqbyrTxXPBjMHXqay7ZpaL8XG6eC6YMUg5pdEruUfkfvG8MGOQcpYuXgVmDFLO5m4HuV+sAmYMQvUDuXoVkpWcCPqSXSZjUOjZJaSL1WDGklWdMSh9X8wqpIslqOeSZZlXSBHNfNc4V/pBjdRtyXI9KKFj1nEKd164mgQFukhQoIsEBbpIUKCLBAW6SFCgiwQFukhQ+AcnNqhhppuiTAAAAABJRU5ErkJggg==" alt="" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<ul>
<li><input type="radio" name="g" value="11" />男</li>
<li><input type="radio" name="g" value="22" />女</li>
<li><input type="radio" name="g" value="33" />女司机</li>
</ul>
<!--radio = document.getElementsByTagName('input')
[<input type="radio" name="g" value="11">, <input type="radio" name="g" value="11">, <input type="radio" name="g" value="11">]
radio[1]
<input type="radio" name="g" value="11">
xo = radio[1]
<input type="radio" name="g" value="11">
xo.value
"11"
xo.checked
false
xo.checked = true
true-->
</body>
</html>
代码及讲解
案例三:克隆
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAo4AAAClCAIAAABdmOtrAAATeUlEQVR4nO3dv2/iSB/H8e9/4XLLLVOSjuI5KSXXpdkHSm6rVHu0aBtXz6VEui2QaLiO4lai5I7GzUWU0VaIZilRKqQNhqfABv+YGY8Nucwu75cobnPDYBwpH8/4O2PZAQAAh8lrHwAAADAhqgEAcBpRDQCA04hqAACcRlQDAOA0ohoAAKcR1QAAOI2oBgDAaUQ1AABOI6oBAHAaUQ0AgNOIagAAnEZUAwDgNKIaAACnEdUAADiNqAYAwGmFUb1+HPmt2hsREZE3tXrLHz2ucq1WQa9989YTERHvbb3R7k2WFbsqaz689USkMcx/YOKjA78mIuIHpzbSmPn7L5/j+bNDo+XoVt1IbkfLcl0BAC6GOaqj8LrxJ8vdbrea9W89EZGa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X+99uEAALSI6suy/fJl89tv33766RDSRDUAOI6ovgiHie5MQhPVAOA+ovoHl5noJqoB4LtTGNXrx5Hfqr0RERF5U6u3/NHjKtdqFfTaN289ERHx3tYb7d5kWbGrsubDW09EGsP8ByY+OvBrIiJ+cGojjZm///I5nj87NFqObtWN5Ha0LNdVEd1EN1ENAN8dc1RH4XXjT5a73W416996IiI1P1gnWu3D8qo9fFztduvluFMTEfFuh/PSXZUWBbU5quMMNqawVSOtwBeRVm+SM1sev99y2BCpd0b5VslLFquuNAonunWvbz/99Pz+/ea33zZ//BE+PGyfnsqfAwDAizBGdTwKPGbXenLn7Qd4xziLRoGJrJz3b7LxaddVWYegNkX1MYMNKWzVyCDwLd64HDaKhv+2XeVYTnSXCO+ff35+/37z6VP4+XP48FDycAAAZ2OO6mFjP1l9P8v+KBE4gb9PuNZolf3RMXDsuipnHnVg7GUddK6OrTQJaNXIZDm6Fbm6L5ignvmeSHtsnvS36ypWdqL7XOG9/fLF8tQAAE5UcK96OZtMJsH8GC5xviYDZzUPJpPJLBGVUVSnAseqqxL2QX3T73cMUb3PYO+u32voU9iqkdkZx8t2XcXCh4dvP//8L+S0OrzfvXv+9dfNp0/hX38R3gDwQkpWgK9GLRER725ium06u78SEbnpz0/uSmfev4luh0eXBapw28+x1/xgHV8WKHLSqlGR5bBhcc0R+BbjZbuu0jafPn37z39eK7C14f31a5kvAQBQs4/q9epx1Kl7Im9a/Udtuq7nQa/1RsSrdybatLHryiAK6tFyt9NHdZzBs/VxBJ9LYatGxWa+J9IZju/iMnhVgftydCvS6I16cRm8slbeqqu87devzx8+vHpUK17v3z9//Lj59Cl8eCC8AaACu6iO7z2L1Nr9QDM3uzzcOn5744/mmmix6spsdl8TuepEpeOaqF6N255II6pC16SwVSMb0UF4t71AX+Ae91/rjOb6WnmrrnTCh4dv//3v68ezTXhTag4AdkpMgK+Xj2P/xhORWns81zVazYN+uyYi3s39TJctVl1pzO5rqdxSRvVq3PaSEahMYatGdlZB/75z10t84bjA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nMVRnT0SdVQf959JbHraDU0rs03XIrlzRR4DjiKqoY7q5FxoFAylw7JyVE8zy7rUUT0dFITQsUHqvqzlRij7b51813N3mqmQt7uGsIjq7DvyVXWpSwTFPWZNTYDdOvLohO9/6WGXm9OAW4hqqIMk86e/UlpXjercmFL15AxzeXPmQFJj1hJR3RyEx49uhotp5j5xevPRgWZq+ixRbXrWma7QzO6bLsJm9JuNr8+0jxUB8CqI6ounWJp1+tOfor6qRXXYvX5uprYNrzgBrnm7bteRcKp5V/ww6bDbDY3XDfobzOWiuuzDOk+I6mNO75JTKac8qQXAuRHVF+yw31acJUWJaChAU39AlaiOLx2S8XNaVGdXJWk3CEv/0OZDX2gC3Caq4yuG7LlKKorq7I2GVAV4dPeawm/g9RHVlyku7T7+HU+Nrs75KSWjOre5h801hEk+9tQBNu2+ZFSXq6bWLppKFn9lZh3KRrVq+iT3uYcjJ7CB10RUX6ypYktL+8XNVsFZPqoV25rGaVEpqpWP01AWYWWKyHaVo1qxl6dlBXiyLj13hIdfjep8lryXr70sU14ipMrLz7D1CoDSiGqomDLbfg68ZFQrcjrdleHSIfcpmeVeSdpg05eMRYdnv/44FZCqqO4qvuk2VcKW/b/hVHsm7SvAzY/sNI3mCWng9RDVeDmLsGk7cVpys8xkcqc/Ig7U0wuY1Q/UqiBXsGZS6fEYuonucFBuKiLsUvgNuIioBgDAaUQ1AABOI6oBAHAaUQ0AgNOIagAAnEZUAwDgtP8DlRTDkB3f9MQAAAAASUVORK5CYII=" alt="" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h2 id="h1">333
<span>123</span>
<a>123</a>
</h2>
<div id="container">
<div class="item">1 </div>
<div class="item">2</div> </div>
<script> var h = document.getElementById('h1');
var c = document.getElementById('container')
//c.appendChild(h);//直接添加到2下面了
// var newH = h.cloneNode(true);//不加true,只克隆格式
// c.appendChild(newH);加true后完整克隆到2下面了
</script>
</body>
</html>
代码及注释
案例四:自定义属性,获取自定义属性并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w9NuOt76kcJzDL8gruGOrs5038pwZ0fdI0DH+4Zn32Rz6TU0zSvcxvALUv6Vp6eOIlF8FIk3z42qwrMyRFRVLZohinKEjAqsWgswHJYU/iqOZkZ2qh1muFPIMQGO4VHXnPERilYZLmqEsWk5JY42lmbDi6cD5tF7Ga5lFoanpxJ6OcBwqKXacDHtxNlvNnuU0pauD+0UsOym3n5ry49GL8syC4bDEmvCza9ZP509PDvhsin6qFg1nOlrxmiiAqPBcQw3u9A8XP4QwWBsYIU5b8Tup3Ppba2nDRvSX3L/CqVHGL2vCb91gJHBcICRwXCAkcFwgJHBcICRwXCAkcFwgJHBcICRwXCAkcFwgJHBcICRwXCAkcFwgJH5P6mFJ+qwgKqDAAAAAElFTkSuQmCC" alt="" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body> //可以构造出一种选择器 <input type="button" onclick="Func()" value="点我啊" />
<div id='i1'> <div class="c1">123</div>
<div class="c1" alex='sb'>123</div>
<div class="c1" alex='sb'>123</div>
<div class="c1" alex='sb'>123</div>
<div class="c1">123</div>
<div class="c1" alex='sb'>123</div>
<div class="c1">123</div>
<div class="c1" alex='sb'>123</div>
<div class="c1">123</div>
</div>
<script>
function Func(){
//i1所有孩子,循环每一个孩子,判断如果alex='sb'
var i1 = document.getElementById('i1')
var divs = i1.children
for ( var i =0; i<divs.length;i++){
var current_div = divs[i];
var result = current_div.getAttribute('alex')//得到属性
//console.log(result);
if (result == 'sb'){
current_div.innerText = '456';
}
}
}
</script>
</body>
</html>
案例代码
案例五:获取输入框内的值三种方法
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAATEAAAB6CAIAAABY7/MkAAAIrklEQVR4nO3dvU8j1xrH8fNfULLdltAt3RZBShNp6SgS7S0jV1Sh3MLNVtltkA0MEEAKZqs0llKyHQ0WZURBiHI3cJUod8W9ukHy29zCyJmdl2fOjGfOPLa/H6HVeObY2KPnp3P8mPEaH4AmpuonAOATZBLQhUwCupBJQB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qSJsNxbIQp1P63ZB0ATKL060K0fc+AnyWTScvaHGtdwFK5laTwewb8uEyGcpj7GQKTK7e8ss4k8uSTmknLeSxpp/xbUp8GUIhyy0vh9wwkHSWTUKL095PavmfA/mjWpwEUQml52a9dTVybJ7ptPyWSSVRLV3nJ7zwtJz05fknplXdmGgBMQl15RedGuWeTOpfGjo/Orkk7Mw0AJjfd5WWzuE0awCQJnagwQBcyCeiiNJOZlojCZyEF/hbADaVFSSYxtzQWpU3rVRjvBxqkwocrqQ8LVEJjFdp/zhHdbxmwfE1XwAF1VSh8XCHMh0mD7bNHIKGErkKUV5LyGjX20aJ3jF2ysoKFHkrrz3IqS5oMx9EKDRBmVPlXA86Ufl2I5u8ZsLkJOFZu/an9ngHWrlCr3PrLWutyPFIzaZk05kloVm79Kf+eAZubgGOlv5/U+T0DrF2hltL6s1+7muzfMwBopqtS5ZlKXojGxo9MYuqoq9To3CivMFPn0uD41FUrK1hUbrorz2ZxC0wXihjQhUwCupBJQBcyCehCJgFdyCSgC5kEdCGTgC5kEtCFTAK6kElAFzIJ6EImAV3IJKALmQR0IZOALmQS0IVMArqQSUAXMgnoQiYBXcgkoAuZBHQhk4AuZBLQhUwCuvwfzb10cHbPMT8AAAAASUVORK5CYII=" alt="" />
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<div id="inp"> </div>
<input type="text" class="aa"/>
<input type="button" value="添加" onclick="Func();" />
</div>
<div id="lis">
<ul>
<li>红烧肉</li>
<li>红烧肘子</li>
<li>红烧鱼</li>
</ul>
</div>
<script>
var inp_aa = document.getElementsByClassName('aa');
inp_aa[0].value = '红烧带鱼';//相当于在输入框输入内容 </script>
</body>
</html>
方法一:类方法
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<div id="inp"> </div>
<input type="text" class="aa"/>
<input type="button" value="添加" onclick="Func();" />
</div>
<div id="lis">
<ul>
<li>红烧肉</li>
<li>红烧肘子</li>
<li>红烧鱼</li>
</ul>
</div>
<script> var c = document.querySelector('.aa');//查询选择器 .是区别id,标签的语法
c.value = '红烧茄子'; </script>
</body>
</html>
方法二:查询
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<div id="inp"> </div>
<input type="text" id="aa"/>
<input type="button" value="添加" onclick="Func();" />
</div>
<div id="lis">
<ul>
<li>红烧肉</li>
<li>红烧肘子</li>
<li>红烧鱼</li>
</ul>
</div>
<script> var c = document.getElementById('aa');
c.value = '红烧茄子'; </script>
</body>
</html>
方法三:id
案例六:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWgAAACOCAIAAACT7QWpAAAHQElEQVR4nO3dP2/bRhjH8XsXHpXNo7xl9JjRo8eOHf0yPGYRRP+vbaBWkg4ZM3rMYkBjJtdFa9RFi9Zoi9QjO6iQGN7dc88jU+Sx+n4gGNTxRAkEn5/uSIVxdwBg5N69+44HDx48TA9XAoARwQHAjOAAYEZwADAjOACYERwAzAgOAGYEBwCzRXC8Bd6+jR0ot7e3b968+bYLk8nk9va2lXKA1hfB0eHnQA6EY+Dq6uqf7lxdXbW5H5BEcGBBOAYuLy8/d+fy8rLN/YAkggMLwjFwcXHxd3cuLi7a3A9IIjiwIBwD5+fnfyk4j99Bs52a8/PzNvcDkggOLMjB8WeEcy7ZolklIDhyQ3BgQTgGzs7OHuOcc8JTueecsP2zs7M29wOSCA4syMHxR4o/T5mJrZ01ztcKWyY4chMKjrsPe682N5xzG5u7xfTJ2ojeEoLj9PT0dx3nXG1BWCt3njs9PW1zPyDJD467b15t7H14LMvyaVpsu933D6ZG9JgQHCcnJ79FOOeCy/7TastsofY05uTkpM39gKTYVOXp8eHTx8ne0O1MHpZoRC8JwXF8fPxrnHNu/re6UFv2+wRf4js+Pm5zPyDJD46nabEzcIOX21/t7X+9/V8c6BvRY0JwHB0d/aLmnEuumi3UnsYcHR21uR+Q5AXHw/tdt118KsuyLO8mO7M40Deiz4TgODw8fBA556rL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cl5zhyuwOYfq31YwDrKaN60E9VXOj8qL+sH1wQHIBJ9/Ugnw1RDh/kjIhFjNxo6gCslSzqwR9lyCc7k6OSYH9/nBJrNHUA1lD/6kEzl4l1YLgBNIKSAGBGcAAwyyg4TDMC4XJsg+8CICijKiI4gL7IpYo0F1aE/mXl8odwfTe5WQAauZSN/lKr365MgeUuqQDwZVE2whVTYWQR66wPCFIDWE73lSNPHOQpSXBr/guDMxQmLMDSMioY5aAgNqyY13+tgzA2kd8aQMxK/nVszncA0zwFIGu+YLK9AxhTFaApzReMtSDlGk4GhzIOGHEADWq+YDK/A5jmKQDZSs5x5HkHMKYqQFMyKhj9VMXZ7wAGoEHdl5b8nS/PO4IZQXAAq5ZFafmjDHlCkRyVVPsnJylMWACr/pWKZi4DYKWoOgBmBAcAM4IDgBnBAcCM4ABgRnAAMCM4AJgRHADMCA4AZhndASz4o29rO4AWZHcHsOcsA2hH93cA81/7/HYAK5XFHcBK7x7lS7cDaEEWdwBjqgL0SxZV5yo32giGgrIdQDtyqbrq1KOWEaZ2AC2g5ACYERwAzAgOAGYEBwAzggOAGcEBwIzgAGBGcAAwIzgAmBEcAMwIDgBmBAcAM4IDgBnBAcCM4ABgRnAAMCM4AJgRHADMCA4AZgQHADOCA4AZwQHAjOAAYEZwADAjOACYERwAzP4Fp1eJ2M2z02gAAAAASUVORK5CYII=" alt="" />
//'beforeBegin'、'afterBegin'\ 'beforeEnd'\'afterEnd'
<!DOCTYPE html>
<html> <head>
<title></title>
<meta charset="utf-8" />
</head> <body>
<div id="inp"> </div>
<input type="text" />
<input type="button" value="添加" onclick="addElen(this)" />
</div>
<div>
<ul id="lis">
<li>红烧肉</li>
<li>红烧肘子</li>
<li>红烧鱼</li>
</ul>
</div>
<script> function addElen(ths) {
var aaa = ths.previousElementSibling.value;
// alert(aaa)
var list_li = document.getElementById("lis");
// alert(list_li)
var bbb = "<li>" +aaa+ "</li>";
// alert(bbb)
list_li.insertAdjacentHTML("beforeEnd", bbb)
}
</script>
</body> </html>
方法一:字符串
<!DOCTYPE html>
<html> <head>
<title></title>
<meta charset="utf-8" />
</head> <body>
<div id="inp"> </div>
<input type="text" />
<input type="button" value="添加" onclick="addElen(this)" />
</div>
<div>
<ul id="lis">
<li>红烧肉</li>
<li>红烧肘子</li>
<li>红烧鱼</li>
</ul>
</div>
<script>
function addElen(ths){
var aaa = ths.previousElementSibling.value; //获取input的值
var list_li = document.getElementById("lis"); //获取ul
var tag = document.createElement('li');//创建li
tag.innerText = aaa; //赋予tag也就是新的li标签里面的值是输入的input的值
list_li.insertBefore(tag, list_li.children[-1])//根据索引插入需要位置的值
}
</script> </body> </html>
方法二:利用方法标签
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div>
<input type="text" />
<input type="button" value="添加" onclick="AddElement(this)" />
</div>
<div>
<ul id="commentList">
<li>alex</li>
<li>eric</li>
</ul>
</div>
<script>
function AddElement(ths){
//获取输入的值
var val = ths.previousElementSibling.value; var commentList = document.getElementById('commentList')
//第一种形式
var str = "<li>" + val + "</li>"
//'beforeBegin'、'afterBegin'\ 'beforeEnd'\'afterEnd'
//beforeEnd 内部最后
//beforeBegin 外部上边
//afterBegin 内部贴身
//afterEnd 外部贴身
// commentList.insertAdjacentHTML("beforeEnd", str)
//第二种方式,元素的方式
var tag = document.createElement('li');
tag.innerText = val;
var temp = document.createElement('a');
temp.innerText = '百度';
temp.href = "http://www.baidu.com";
tag.appendChild(temp);
commentList.insertBefore(tag,commentList.children[1]);
console.log(commentList.children[1])
}
</script>
</body> </html>
方法三:添加的li里面嵌套a标签
案例七:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAASQAAACDCAIAAACr9fFZAAAGx0lEQVR4nO2dTXIjNwxGdVhfIpN7eJPJLpOjZLKOcgN54UtEnUWqXF1N4gPIBqGJ9V5pYXXzByTxSNkuSZe3t7cNANZzQTaAGpANoAhkAygC2QCKQDaAIpANoAhkAygC2QCKQDaAIpANoIhh2S6X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BadM9af/4Uia2ryI5P+VoalNJHYIR+JajlvMLHqmmn+T5ztgU02EggM21UQoOGBTTYSCAzbVRCg4YFNNhIIDNtVEKDhgU02EggM21UQoOGBTTYSCAzbVRCg4YFNNhIIDNtVEKDhgU02EggM21UQoOGBTTYSCAz+mGnO3igWvuxMwhsKmGg1sqtHAphoNbKrRwKYaDWyq0cCmGg1sqtHAphoN/gNm+iOPB2K11gAAAABJRU5ErkJggg==" alt="" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
input{
height: 40px;
}
.gg{
color: gray;
}
.bb{
color: black;
}
</style>
</head>
<body> <p>当鼠标进入是,移除内容</p>
<p>当输入表退出时,添加内容</p>
<input type="text" class="gg" value="请输入内容" onfocus="Focus(this)" onblur="Blur(this)" /> <script>
function Focus(ths){
//查找的第一种方式
//document。。
//第二种方式this
//ths
//ths.className = 'bb'
var current_val = ths.value;
if (current_val == "请输入内容"){
ths.value = "";
ths.className = 'bb';
}
}
function Blur(ths) {
var current_val = ths.value;
if (current_val == "请输入内容" || current_val.trim().length == 0 ){
ths.value = '请输入内容';
ths.className = 'gg';
}
}
</script>
</body>
</html>
案例代码
案例八:头部菜单
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdAAAADJCAIAAAAckfSwAAARmElEQVR4nO3dP0hb+/vA8We4X7h8yy2WSiNeWlu07a1XSZe2wxUsCFekg+2UoVARytelIHWoHQqXLp6lEi4OARFElzNZp+JvEIQuwUm3bM2YMWNGf4Ma8+eckxOffJLj0/fDa2g1SY3Rt59+ck6UE4ZhGKYrI73+ABiGYX6WIbgMwzBdmvaDWxAAuBoSNgQXgF0JG4ILwK6EDcEFYFfChuACsCthQ3AB2JWwIbgA7ErYEFwAdiVsCC4AuxI2BBeAXQkbggvjdk7SqOr5w9FtCRuCC+N63rhE6fnD0W0JG4LbXd9l8aUcfO/1h/Ez6XnjEqXnD0e3JWySFdzSmmSWJN/BHh2LNyP+1zauUjl0+xVQ2ZK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MxyEU3NEFwAZrmIpmYILgCzXERTMwQXgFkuoqkZggvALBfR1AzBBWCWi2hqhuACMMtFNDVDcAGY5SKamiG4AMxyEU3NEFwAZrmIpmYILgCzXERTMwQXgFkuoqkZggvALBfR1AzBBWCWi2hqhuACMMtFNDVDcAGY5SKamiG4AMxyEU3NEFwAZrmIpmYILgCzXERTMwQXgFkuoqmZ/wcKNaMmTna0qAAAAABJRU5ErkJggg==" alt="" />
<!DOCTYPE html>
<html> <head>
<title></title>
<meta charset="utf-8" />
<style>
.pg_top .menu {
background-color: gold;
width: 400px;
height: auto;
position: absolute;
left: 200px;
} .item {
float: left;
margin: 10px 0;
padding: 0 20px;
font-size: 25px;
} .content {
position: absolute;
left: 200px;
top: 58px;
background-color: burlywood;
width: 400px;
height: 200px;
} .hide {
display: none;
} .bk {
background-color: greenyellow;
}
</style>
</head> <body>
<div class="pg_top">
<div class="menu">
<div class="item" con="h1" onclick="Show(this)">十八里店</div>
<div class="item" con="h2" onclick="Show(this)">簋街</div>
<div class="item" con="h3" onclick="Show(this)">后海</div>
</div>
<div id="content" class="content">
<div con="h1">烤全羊</div>
<div class="hide" con="h2">麻辣龙虾</div>
<div class="hide" con="h3">相约酒吧</div>
</div>
</div>
<script>
function Show(ths) {
//当我点这个标签的时候,其他标签的背景色变没
var baba = ths.parentElement.children; //获取到了每个子元素
var target = ths.getAttribute('con');
for (i = 0; i < baba.length; i++) {
if (ths == baba[i]) { //做判断,
ths.classList.add('bk') //添加属性
} else {
baba[i].classList.remove('bk') //删除属性
}
}
//内容
var current_con = document.getElementById('content').children;
console.log(current_con)
for (j=0; j<current_con.length;j++) {
var s_att = current_con[j].getAttribute('con')
if (target == s_att ) {
current_con[j].classList.remove('hide');
}else{
current_con[j].className = 'hide';
}
} }
</script>
</body> </html>
自己写的稀烂代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
ul{
list-style: none;
padding: 0;
margin: 0;
}
ul li{
float: left;
background-color: #2459a2;
color: white;
padding: 8px 10px;
}
.clearfix:after{
display: block;
value:'111';
content: '.';
height: 0;
/*visibility: hidden;*/
clear: both;
}
.hide{
display: none;
}
.tab-menu .title{
background-color: #dddddd;
}
.tab-menu .title .active{
background-color: white;
color: black;
}
.tab-menu .content{
border: 1px solid #dddddd;
min-height: 150px;
}
</style>
</head>
<body>
<div style="width:400px; margin: 0 auto;">
<div class="tab-menu">
<div class="title clearfix">
<ul>
<li target='h1' class="active" onclick="Show(this)">十八里店</li>
<li target='h2' onclick="Show(this)">簋街</li>
<li target='h3' onclick="Show(this)">十刹海</li>
</ul>
</div>
<div class="content" id="content">
<div con="h1">烤羊腿</div>
<div con="h3" <!--class="hide"-->>油焖小龙虾</div>
<div con="h2" <!--class="hide"-->>香甜鸡尾酒</div> </div>
</div> </div>
<script>
function Show(ths){
//ths表示当前标签
var target = ths.getAttribute('target')//h3
//给自己添加样式active
//兄弟们去掉
ths.className = 'active';
var brothers = ths.parentElement.children;
for(var i=0;i<brothers.length;i++){
if ( ths == brothers[i]){ }else{
brothers[i].removeAttribute('class')
}
} //操作内容
var contents = document.getElementById('content').children;
console.log(contents)
for (var j=0;j<contents.length;j++){
var current_content = contents[j];
console.log(current_content)
var con = current_content.getAttribute('con')
if (con == target){
current_content.classList.remove('hide');
}else{
current_content.className="hide";
} } }
</script>
</body>
</html>
稍好点的代码
案例九:返回顶部
主要知识点:onscroll 和document.body.scrollTop;
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAp8AAAD4CAIAAAAl5BqUAAAHhklEQVR4nO3dza3bRhiGUZaRLtKUu7jLICV4FxvO1sHtIJvAP7DTQKqwW1AWARRZHA6HFDWk3nuIs1BkkvezDPgxR5QyvH//BwCQZDidTqfT6dXrb8OP26vX387Gz58fj3cGALo5lbb/6/6fcarHpT8/uHy8+28PAF6g7ete2Q0A6EDdASCNugNAmmV1n624990BYHdNdX9Vug2+eC+9e+YBYHetdW8k6gCwuy3rXvxYPADQ2cbX7gDA7tQdANKoO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+4AkKZc979tNpvNZrNlbcNPv/wDACQZvn79/cuXd58/v/306c3Hj28+fPiNKU9PTx/++vM/T09P+w7z9t2vu78gABzTv8HHQtdQoUwkAAAAAElFTkSuQmCC" alt="" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
.go-top{
position: fixed;
right: 20px;
bottom: 19px;
width: 40px;
height: 40px;
background-color: darkgoldenrod;
color: white;
}
.hide{
display: none;
}
</style>
</head>
<body onscroll="Func();">
<div id="i1" style="height: 2000px; background-color: greenyellow;">
<h1>asdfafdasdf</h1> </div>
<div id="i2" class="go-top hide">
<a href="javascript:void(0)" onclick="goTop();">返回顶部</a> </div>
<script>
function Func(){
var scrollTop = document.body.scrollTop;//body的滚动高度
var ii = document.getElementById('i2');
if(scrollTop>100){
ii.classList.remove('hide');//移除就是显示
}else{
ii.classList.add('hide');
}
}
function goTop(){
document.body.scrollTop=0; }
</script>
</body>
</html>
案例讲解
案例十:
主要知识点:
//scroll 滚动;卷轴的意思
// scrollTop: 滚动条距离顶部高度
// scrollHeight: 文档高度:自身+padding
// clientTop: 边框高度
// clientHeight: 当前范围可视的高度:自身 + padding
//offset 印刷的意思
// offsetTop: 当前标签距离"顶部"的高度(body)
// 如果他的上一级没有postion,如果有则按照position的标签为主
// offsettHeight: 自身范围的高度:自身+padding+border
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div style="height: 20px;background-color: green;"></div><!--头部-->
<div style="border: 5px solid pink;padding: 10px;"><!--body-->
<div>
<div id="xo" style="height: 200px;overflow: auto;width: 400px;margin: 0 auto;border: 15px solid red;padding: 3px;" >
<div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div>
<div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div>
<div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div>
<div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div>
<div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div>
<div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div>
<div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div><div>sdf</div>
<div>sdf</div><div>sdf</div>
</div>
</div>
</div>
<script>
var ii = document.getElementById('xo')
console.log(ii.scrollTop)
console.log(ii.scrollHeight)
console.log(ii.clientTop)
console.log(ii.clientHeight)
console.log(ii.offsetTop)
console.log(ii.offsetHeight)
//scroll 滚动;卷轴的意思
// scrollTop: 滚动条距离顶部高度
// scrollHeight: 文档高度:自身+padding
// clientTop: 边框高度
// clientHeight: 可见范围的高度:自身 + padding
//offset 印刷的意思
// offsetTop: 当前标签距离"顶部"的高度(body)
// 如果他的上一级没有postion,如果有则按照position的标签为主
// offsettHeight: 可见范围的高度:自身+padding+border
</script>
</body>
</html>
测试及答案讲解