
批改狀態(tài):合格
老師批語:寫得很好, 總結(jié)也到位.... 明天開始進(jìn)行第三階段laravel學(xué)習(xí), 建議作業(yè)進(jìn)度與課程一致
一、常用屬性操作知識點(diǎn):
1、屬性:
2、html屬性:
3、css屬性:
4、表單操作
val():獲取或者設(shè)置表單元素的值
5、元素的內(nèi)容
6、元素的自定義屬性值:
二、jQuery對DOM操作
1、插入和替換
append()|appendTo():尾部插入元素
prepend()|prependTo():頭部插入元素
after()|inserAfter():后面插入元素
befor()|insertBefor():前面插入元素
replaceWith()|replaceAll():替換目標(biāo)元素內(nèi)容
2、復(fù)制/克隆元素
clone():創(chuàng)建并返回每一個選中元素的副本
3、刪除元素
empty():將當(dāng)前元素的所有子元素清空/刪除
remove():將當(dāng)前元素以及素有的子元素全部刪除
4、其他知識點(diǎn):
toLowerCase();字符串全部轉(zhuǎn)小寫;
1.1、代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="http://upcdn.b0.upaiyun.com/libs/jquery/jquery-2.0.3.min.js"></script>
<title>Document</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
color: #666;
}
form {
width: 400px;
padding: 20px 10px;
border: 1px solid #aaa;
box-sizing: border-box;
box-shadow: 0 0 5px #888;
background-color: #eee;
display: grid;
grid-template-columns: 80px 200px;
gap: 10px;
place-content: center center;
}
button {
grid-column: 2 / 3;
height: 26px;
}
button:hover {
color: white;
background-color: #000;
border:none;
cursor: pointer;
}
.red {
color: red;
}
</style>
</head>
<body>
<h2 class="red">用戶登錄</h2>
<form action="handle.php" method="post">
<label for="email">Email:</label>
<input type="email" name="email" id="email" autofocus value="lectur@php.cn">
<label for="password">Password</label>
<input type="password" name="password" id="password" placeholder="不少于6位">
<label for="confirm">記住我:</label>
<div>
<input type="radio" name="save" id="confirm" value="1" checked><label for="confirm">保存</label>
<input type="radio" name="save" id="cancel" value="0"><label for="cancel">取消</label>
</div>
<button>登錄</button>
</body>
<script>
var form=$('form');
// 1、attr() 一個參數(shù)事獲取屬性值,兩個參數(shù)(屬性名,新的屬性值)
console.log(form.attr('action'));
// console.log(form.attr('action','admin.php'))
// 同時操作多個,可以用對象字面量作為參數(shù)
// form.attr({
// 'action':'admin.php',
// 'method':'get'
// })
form.attr('method','GET');
form.attr('method',function(){
var method=$(this).attr('method').toLowerCase();
if(method==='get'){
$(this).attr('action','query.php?id=1');
}
return 'POST';
})
form.removeAttr('action');
// 2、關(guān)于CSS的操作
console.log(form.css('width'));
console.log(form.css('border'));
console.log(form.css('border-style'));
console.log(form.css('border-color'));
$('h2').removeClass('red');
$('h2').addClass('red');
$('button').toggleClass('red');//如果有則刪除,沒有則添加
// $('button').toggleClass('red');
if($('button').hasClass('red')){
console.log('有red類樣式');
}
$('#email').val('ldy@php.com');//并沒有改變html中的值,只是在渲染html頁面時替換了
$('#email').val(function(){
return this.defaultValue;
});
$('h2').text('登陸表單');//添加文本內(nèi)容,無法識別html標(biāo)簽元素
$('h2').html('登錄<span style="color:green">表單</span>');//可以識別html元素;
var form=document.forms.item(0);
console.log(form.getBoundingClientRect());//包含x,y坐標(biāo),上下左右(距父級起點(diǎn)的距離)和寬高
console.log($('form').offset());//只有上,左,離頂部的距離
$('body').css('height','2000px');
// $(document).on('scroll',function(){
// console.log($(document).scrollTop());
// });
form.dataset.src='1';
console.log($(form).data('src'));
// $(form).data('src','2');
$(form).removeData('src');
// $(form).data('src', 'PHP中文網(wǎng)歡迎你');
</script>
</html>
1.2、運(yùn)行結(jié)果圖:
2.1代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="http://upcdn.b0.upaiyun.com/libs/jquery/jquery-2.0.3.min.js"></script>
<title>Document</title>
<style>
.action{
color:red;
}
</style>
</head>
<body>
<script>
$('body').append('<ol>');
// $('body').prepend('<ol>');
// $('<li>').text('華為手機(jī)').appendTo('ol:last-child()');
// $('<li>').text('小米手機(jī)').prepednTo();
$('<li>').text('小米手機(jī)').appendTo('ol:first-child()');
$('<li>').addClass('action').html('<span>智能手機(jī)</span').appendTo('ol');
// 添加元素的同時可以添加樣式屬性
$('<li>', {
'id': 'test',
'style': 'background-color: yellow'
}).html('<a href="" alt="">最新男裝</a>').appendTo('ol');
$('ol').append(function(){
var lis='';
for(var i=0;i<5;i++){
lis+='<li>最新產(chǎn)品'+(i+1)+'</li>';
}
return lis;
});
$('ol > li:nth-child(3)').before('<li>新元素1</li>');
$('ol > li:nth-child(3)').after('<li>新元素2</li>');
$('<li>新元素3</li>').insertBefore('ol > li:first-child()');
$('ol>:last-of-type()').replaceWith('<h4>替換元素</h4>');
$('<h4>我才是第一個</h4>').replaceAll('ol > li:first-of-type');
var ul=$('<ul>').appendTo('body');
ul.append(function(){
return $('ol:first-of-type > li:nth-of-type(-n+4)').clone();
});
console.log($('ol>li:nth-of-type(-n+2)'));
// $(ul).empty();
$(ul).remove();
</script>
</body>
</html>
2.1運(yùn)行效果圖:
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號