实验目的及要求:
- 掌握Jquery页面初始化方法
- 掌握Jquery的选择器的基本使用
- 掌握Jquery对DOM 的基本操作
- 掌握vue的基础用法
1
要求:1.奇数行背景色:#FFF38F;偶数行背景色:#FFFFEE;选中行:#FF6500
2.选中行时,设置单选框选中状态
<!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>
body,html{
border:0;
padding:0;
}
.table{
border:1px solid #000;
border-collapse: collapse;
text-align: center;
border-spacing: 0;
}
td,th{
border:1px solid #000;
}
td{
width: 80px;
}
td:last-child{
width:140px;
}
tr:nth-child(odd){
background-color: #ffffee;
}
tr:nth-child(even){
background-color: #fff38f;
}
tr.selected{
background-color: #ff6500;
}
</style>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<body>
<div id="app">
<table class="table" @click="choose($event)">
<tr>
<th></th>
<th>姓名</th>
<th>性别</th>
<th>暂住地</th>
</tr>
<tr v-for="(item,index) in list" :key="item.id">
<td><input v-model="selected" type="radio" :id="item.id" /></td>
<td>{{item.name}}</td>
<td>{{item.sex?'男':'女'}}</td>
<td>{{item.place}}</td>
</tr>
</table>
</div>
</body>
<script>
new Vue({
el: "#app",
data: {
list:[
{
id: 1,
name: '张三',
sex: 1,
place: '四川成都'
},
{
id: 2,
name: '李四',
sex: 0,
place: '四川绵阳'
},
{
id: 3,
name: '夏彦',
sex: 0,
place: '四川成都'
},
{
id: 4,
name: '王五',
sex: 1,
place: '四川南充'
},
{
id: 5,
name: '赵六',
sex: 1,
place: '四川自贡'
},
{
id: 6,
name: '罗梅',
sex: 1,
place: '四川德阳'
},
],
selected: ''
},
methods:{
choose(e){
const radio = e.target.closest('tr').querySelector('input[type=radio]');
if(radio){
radio.checked = !radio.checked;
this.selected = radio.id
radio.checked?e.target.closest('tr').style.backgroundColor = '#FF6500':e.target.closest('tr').style.backgroundColor =
radio.id%2===0?'#ffffee':'#fff38f'
console.log(radio.id);
}
}
}
});
</script>
</html>
2
Jquery中提供了filter方法进行过滤,如$("选择器").filter(内容过滤器)。搜索过程中可先隐藏hide所有数据行,满足条件的行进行显示show()
<!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>
body,html{
border:0;
padding:0;
}
.table{
border:1px solid #000;
border-collapse: collapse;
text-align: center;
border-spacing: 0;
}
td,th{
border:1px solid #000;
}
td{
width: 80px;
}
td:last-child{
width:140px;
}
tr:nth-child(odd){
background-color: #ffffee;
}
tr:nth-child(even){
background-color: #fff38f;
}
tr.selected{
background-color: #ff6500;
}
</style>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<body>
<div id="app">
<div class="search">
<label>查询</label>
<input type="text" v-model="key">
</div>
<table class="table" @click="choose($event)">
<tr>
<th></th>
<th>姓名</th>
<th>性别</th>
<th>暂住地</th>
</tr>
<tr v-for="(item,index) in searchList" :key="item.id">
<td><input v-model="selected" type="radio" :id="item.id" /></td>
<td>{{item.name}}</td>
<td>{{item.sex?'男':'女'}}</td>
<td>{{item.place}}</td>
</tr>
</table>
</div>
</body>
<script>
new Vue({
el: "#app",
data: {
list:[
{
id: 1,
name: '张三',
sex: 1,
place: '四川成都'
},
{
id: 2,
name: '李四',
sex: 0,
place: '四川成都'
},
{
id: 3,
name: '王五',
sex: 0,
place: '未知'
},
{
id: 4,
name: '赵六',
sex: 1,
place: '四川自贡'
},
{
id: 5,
name: '心理学家',
sex: 0,
place: '第五医院'
},
{
id: 6,
name: '王五',
sex: 1,
place: '四川德阳'
},
],
selected: '',
key:''
},
methods:{
choose(e){
const radio = e.target.closest('tr').querySelector('input[type=radio]');
if(radio){
radio.checked = !radio.checked;
this.selected = radio.id
radio.checked?e.target.closest('tr').style.backgroundColor = '#FF6500':e.target.closest('tr').style.backgroundColor =
radio.id%2===0?'#ffffee':'#fff38f'
console.log(radio.id);
}
}
},
computed:{
searchList() {
return this.list.filter(item => {
return item.name.includes(this.key);
});
},
},
});
</script>
</html>
3
<!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>select</title>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<body>
<div id="app">
<select name="province" id="province" @change="handleChange($event,$event.target.value)">
<option disabled selected>请选择省份</option>
<option v-for="p in list" :value="p.id">{{p.name}}</option>
</select>
<select v-show="selected.province" name="city" id="city" @change="handleChange($event,$event.target.value)">
<option disabled selected>请选择城市</option>
<option v-for="c in list[key].city" :value="c.id">{{c.name}}</option>
</select>
</div>
</body>
<script>
new Vue({
el: "#app",
data: {
list: [
{
id: '001',
name: '四川省',
city: [
{
id: '001001',
name: '成都市',
county: ['...']
},
{
id: '001002',
name: '巴中市',
county: ['...']
},
{
id: '001003',
name: '绵阳市',
county: ['...']
},
]
},
{
id: '002',
name: '河北省',
city: [
{
id: '002001',
name: '邢台市',
county: ['...']
},
{
id: '002002',
name: '石家庄',
county: ['...']
},
{
id: '002003',
name: '邯郸市',
county: ['...']
},
]
},
{
id: '003',
name: '广东省',
city: [
{
id: '003001',
name: '珠海市',
county: ['...']
},
{
id: '003002',
name: '深圳市',
county: ['...']
},
{
id: '003003',
name: '广州市',
county: ['...']
},
]
},
{
id: '004',
name: '未名省',
city: [
{
id: '003001',
name: '北区',
county: ['...']
},
{
id: '003002',
name: '嘉南区',
county: ['...']
},
{
id: '003003',
name: '锦兰区',
county: ['...']
},
{
id: '003004',
name: '文华区',
county: ['...']
},
{
id: '003005',
name: '滨河南区',
county: ['...']
},
]
},
],
selected: {
province: '',
city: ''
},
key: 0
},
methods:{
handleChange(e,value){
this.key = Number(value)-1;
switch(e.target.id){
case 'province':
this.selected.province = value;
break;
case 'city':
this.selected.city = value;
break;
default:
console.log('changed')
}
}
},
});
</script>
</html>
4
<!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>
body,html{
border:0;
padding:0;
}
.table{
border:1px solid #000;
border-collapse: collapse;
text-align: center;
border-spacing: 0;
}
td,th{
border:1px solid #000;
}
td{
width: 80px;
}
td:last-child{
width:140px;
}
tr:nth-child(odd){
background-color: #ffffee;
}
tr:nth-child(even){
background-color: #fff38f;
}
.submit{
position: relative;
left:40vw;
}
</style>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<body>
<div id="app">
<fieldset>
<legend>添加:</legend>
<dialog id="tip">请正确输入所有信息</dialog>
Name: <input type="text" v-model="person.name" /> Email: <input type="text" v-model="person.email" />
Salary:
<input type="text" v-model="person.salary" /> <br /><br />
<button class="submit" @click="add">Submit</button>
</fieldset>
<br /><br />
<table class="table">
<tr>
<th>Name</th>
<th>Email</th>
<th>Salary</th>
<th></th>
</tr>
<tr v-for="(item,index) in list" :key="item.id">
<td>{{item.name}}</td>
<td>{{item.email}}</td>
<td>{{item.salary}}</td>
<td><a href="#" @click="del(item.id)">Delete</a></td>
</tr>
</table>
</div>
</body>
<script>
var app = new Vue({
el: "#app",
data: {
list: [
{ id: 1, name: "李四", email: "ls@qq.com", salary: "8888" },
{ id: 2, name: "陆景和", email: "ljh@qq.com", salary: "1000000" },
{ id: 3, name: "夏彦", email: "xy@qq.com", salary: "50000" },
{ id: 4, name: "左然", email: "zr@qq.com", salary: "30000" },
{ id: 5, name: "张三", email: "zhangsan@qq.com", salary: "4000" },
],
curId: 4,
person: {
id: null,
name: "",
email: "",
salary: "",
},
},
methods: {
add() {
if(Object.values(this.person).some(i=>i==='')){
let t = document.getElementById('tip')
t.open = true
setTimeout(()=>{t.open=false},1000)
return;
}
this.person.id = this.curId++;
this.list.push(this.person);
this.person = { id: null, name: "", email: "", salary: "" };
},
del(id){
this.list = this.list.filter(i=>i.id!==id)
}
},
});
</script>
</html>