取赋值相关方法:val
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>温故而知“心”</title>
<style>
</style>
</head>
<body>
<form action="" method="GET">
<h3>选择你喜欢的明星</h3>
<select name="star" id="">
<option value="s">乔丹</option>
<option value="a">詹姆斯</option>
<option value="b">库里</option>
</select>
<h3>一句话简单介绍你喜欢的明星</h3>
<input type="text" name="easy" value="cst">
<h3></h3>
三分准:<input type="checkbox" name="special" value="three">
组织好:<input type="checkbox" name="special" value="org">
突破强:<input type="checkbox" name="special" value="strong">
<h3>具体描述该明显的技术特点</h3>
<textarea name="des" id="" cols="30" rows="10"></textarea>
<input type="submit" value="login">
</form>
<div></div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
console.log($('input[type=checkbox]').eq(1).val())
console.log($('input[type=checkbox]').val())
console.log($('form').serialize())
console.log($('form').serializeArray())
$('input[type=checkbox]').val(function (index, oldValue) {
return oldValue + index
})
$('input[type=checkbox]').eq(1).val('xyz')
$('div').val('xyz');
console.log('查看div里面的vaule值:', $('div').val())
</script>
</html>
next和prev对比看
next
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>温故而知“心”</title>
<style>
</style>
</head>
<body>
<button>change</button>
<span class="demo">xyz</span>
<p class="demo">lm</p>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('button').click(function () {
$(this).next('p').css({ fontSize: "30px", color: "#ff0000" })
})
</script>
</html>
prev
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>温故而知“心”</title>
<style>
</style>
</head>
<body>
<span class="demo">xyz</span>
<p class="demo">lm</p>
<button>change</button>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('button').click(function () {
$(this).prev('p').css({ fontSize: "30px", color: "#ff0000" })
})
</script>
</html>
nextAll 和 nextUntil
nextAll
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>温故而知"心"</title>
<link rel="stylesheet" href="./index.css">
</head>
<body>
<div class="wrapper">
全选:<input type="checkbox">
banana:<input type="checkbox">
apple:<input type="checkbox">
orange:<input type="checkbox">
<input type="submit" value="login">
</div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('input[type="checkbox"]').eq(0).click(function () {
if ($(this).prop('checked')) {
$(this).nextAll('input[type="checkbox"]').prop('checked', true);
} else {
$(this).nextAll('input[type="checkbox"]').prop('checked', false);
}
})
</script>
</html>
nextUntil
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>温故而知“心”</title>
<style>
</style>
</head>
<body>
<div class="wrapper">
<h1>水果</h1>
全选:<input type="checkbox">
banana:<input type="checkbox">
apple:<input type="checkbox">
orange:<input type="checkbox">
<h1>nba</h1>
全选:<input type="checkbox">
Rose:<input type="checkbox">
Curry:<input type="checkbox">
James:<input type="checkbox">
<input type="button" value="submit">
</div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('h1').next().click(function () {
if ($(this).prop('checked')) {
$(this).nextUntil('h1', 'input[type="checkbox"]').prop('checked', true);
} else {
$(this).nextUntil('h1', 'input[type="checkbox"]').prop('checked', false);
}
})
</script>
</html>
siblings
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>温故而知“心”</title>
<style>
</style>
</head>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
</ul>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('li')
.eq(1).css({ backgroundColor: '#ff0000', fontSize: '30px' })
.siblings().css({ backgroundColor: '#0000ff', fontSize: '10px' })
</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>温故而知“心”</title>
<style>
</style>
</head>
<body>
<ul>
<span>span1</span>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<span>span2</span>
<li>5</li>
<li>6</li>
<li>7</li>
<span>span3</span>
</ul>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('li')
.eq(1).css({ backgroundColor: '#ff0000', fontSize: '30px' })
.siblings('span').css({ backgroundColor: '#0000ff', fontSize: '10px' })
</script>
</html>
parent & parents & closest & offsetParent
parent
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>温故而知“心”</title>
<style>
</style>
</head>
<body>
<div class="shop" data-id="101">
<p>basketball-nike</p>
<button>add</button>
</div>
<div class="shop" data-id="102">
<p>basketball-adidas</p>
<button>add</button>
</div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('button').click(function () {
console.log($(this).parent()
)
})
</script>
</html>
parents
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>温故而知“心”</title>
<style>
</style>
</head>
<body>
<div class="shop" data-id="101">
<div>
<p>basketball-nike</p>
<button>add</button>
</div>
</div>
<div class="shop" data-id="102">
<div>
<p>basketball-adidas</p>
<button>add</button>
</div>
</div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
console.log($('button').eq(0).parents())
console.log($('button').eq(0).parents('.shop'))
</script>
</html>
closest
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>温故而知“心”</title>
<style>
</style>
</head>
<body>
<div class="wrapper">
<ul>
<li>
<ul data-id="101">
<li>nike</li>
<li>200$</li>
<li>
<button>add</button>
</li>
</ul>
</li>
<li>
<ul data-id="102">
<li>nike</li>
<li>200$</li>
<li>
<button>add</button>
</li>
</ul>
</li>
</ul>
</div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
console.log($('button').eq(0).closest('button'))
console.log($('button').eq(0).closest('ul').attr('data-id'))
</script>
</html>
offsetParent
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>温故而知“心”</title>
<style>
.wrapper {
position: relative;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="box">
<span>123</span>
</div>
</div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
console.log($('span').offsetParent())
</script>
</html>
slice
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>温故而知“心”</title>
<style>
</style>
</head>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
</ul>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
console.log($('li').slice(3, 5).css({ backgroundColor: "#ff0000" }))
</script>
</html>