批改状态:未批改
老师批语:
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>zuoye1</title>
</head>
<body>
<img src="../images/zly.jpg" width="200" alt="美女" title="明星" id="pic" data-nation="中国">
</body>
</html>
<script type="text/javascript" src="../js/jquery-3.3.1.js"></script>
<script type="text/javascript">
var res = $('#pic').attr('src')
// 图片由zly替换为gyy
$('#pic').attr('src', '../images/gyy.jpg')
// 添加一个值:style
$('#pic').attr('style', 'border-radius: 50%;box-shadow:2px 2px 2px #888')
//可以通过data-前缀给标签添加用户自定义属性
var res = $('#pic').attr('data-nation')
//attr()的属性值,还支持回调函数,会自动转为字符类型再赋值给width属性
$('#pic').attr('width', function(){return 100+50})
// 控制台查看width
// var res = $('#pic').attr('width')
//删除图片的内联样式属性style
// $('#pic').removeAttr('style')
//可以删除多个属性,多个属性之间用空格分开,返回当前元素的状态
// var res = $('#pic').removeAttr('alt title data-nation')
//在控制台查看运行结果
console.log(res)
</script>点击 "运行实例" 按钮查看在线实例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>zuoye2</title>
<style type="text/css">
.box1 {
width: 300px;
height: 300px;
background-color: lightblue;
position: relative;
}
.box2 {
width: 100px;
height: 100px;
background-color: pink;
position: absolute;
top: 50px;
left: 100px;
}
</style>
</head>
<body>
<img src="../images/jsy.jpg">
<div class="box1">
<div class="box2"></div>
</div>
</body>
</html>
<script type="text/javascript" src="../js/jquery-3.3.1.js"></script>
<script type="text/javascript">
//设置样式
// var res = $('img').css('width',200)
// var res = $('img').css('border-radius', '10%')
// var res = $('img').css('box-shadow', '3px 3px 3px #888')
// 链式写法
// var res = $('img').css('width','200px')
// .css('border-radius', '10%')
// .css('box-shadow', '3px 3px 3px #888')
// 简写
var res = $('img').css({
'width': '200',
'border-radius': '10%',
'box-shadow': '3px 3px 3px #888'
})
//读取样式,返回的都是字符串类型
var res = $('img').css('box-shadow')
// 获取宽度
var res = $('img').css('width')
//因为返回的是字符串,所以对于宽高等数据,如果要计算,就必须先转为数值型 =200
var res = parseInt($('img').css('width'))
// 计算 =250
res += 50
// 图片宽度动态修改
var res = $('img').css('width',res+'px')
// width()和height()方法
// 获得图片高度或者宽度
// var res = $('img').css('height')
var res = $('img').height()
// 将图片高度或者宽度设置赋值,单位默认为px
var res = $('img').height(200)
// 等价于:var res = $('img').css('height',200)
// 设置宽高支持运算符的简写
// var res = $('img').width('+=100')
// var res = $('img').width() //350
// 等价于:var res = $('img').css('width','+=100')
// 获取元素的位置:offset(),返回的是一个对象
// var res = $('img').offset()
// 查询距离左边和顶部的偏移量
// var res = $('img').offset().left
// var res = $('img').offset().top
// 这个操作反映的是元素在普通文档流的位置
// 查看绝对定位元素的偏移量: position()
// var res = $('.box2').position().left
// var res = $('.box2').position().top
//offset()和position()方法仅适用于页面中的可视元素,并且仅能获取,不能设置
//控制台查看结果
console.log(res)
</script>点击 "运行实例" 按钮查看在线实例



Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号