**1.使用jquery修改div元素的背景色(随意颜色)**
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div style="width: 500px;height: 500px;background-color: orangered;"></div>
<button id="change">切换颜色</button>
<script src="../jquery-3.4.1.min.js"></script>
<script>
$(function () {
$('#change').click(function () {
$('div').css("background","yellow");
})
})
</script>
</body>
</html>
**2.使用jquery修改div的子元素p的内容为"我是子元素"**
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div style="width: 200px;height: 200px; background-color: yellowgreen;">
<p>我不要改名字</p>
<p>我也不要改名字</p>
</div>
<script src="../jquery-3.4.1.min.js"></script>
<script>
$(function () {
$('div').text("我是子元素");
})
</script>
</body>
</html>
**3.使用jquery修改第二个p元素的背景色为"orange"**
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<p style="background-color: yellowgreen;">我是第一个</p>
<p style="background-color: yellow;">我是第二个</p>
</div>
<button id="change">切换第二个P标签颜色</button>
<script src="../jquery-3.4.1.min.js"></script>
<script>
$(function () {
$('#change').click(function () {
$('P:eq(1)').css("background","orange");
})
})
</script>
</body>
</html>
**4.删除列表元素中最后一个li元素**
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<button id="delete">删除最后一个li</button>
<script src="../jquery-3.4.1.min.js"></script>
<script>
$(function () {
$('#delete').click(function () {
$('li:last').remove();
})
})
</script>
</body>
</html>
**5.点击当前的p标签,弹出相对应的元素下标**
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<script src="../jquery-3.4.1.min.js"></script>
<script>
$(function () {
$('p').click(function () {
alert($(this).index());
})
})
</script>
</body>
</html>
**6.利用jquery中的animate()方法.点击id为btn的按钮,实现类名为box的元素,向左移动500px**
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<button id="btn">移动box</button>
<div class="box" style="width: 500px;height: 200px;background-color: brown;left: 500px;position: absolute;">123</div>
<script src="../jquery-3.4.1.min.js"></script>
<script>
$(function () {
$('#btn').click(function () {
$('.box').animate({"left":"-=500px"});
})
})
</script>
</body>
</html>
Jquery练习
最新推荐文章于 2022-10-13 09:14:07 发布