批改状态:合格
老师批语:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<img src="../img/1.png" width="200" id="pic" title="美女" alt="头像" data-nation="中国">
</body>
</html>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
// 获取id,传入参数src,必须传,不然会出错
var res = $('#pic').attr('src')
// 然后就随便你修改
$('#pic').attr('src', '../img/2.png')
$('#pic').attr('style', 'box-shadow:0px 0px 5px #888')
// 还可以通过回调函数修改 : function(){return 100+50}
$('#pic').attr('width', function(){return 100+50})
//添加自定义属性
var res = $('#pic').attr('data-nation')
// 查看属性,爱查哪个传哪个
var res = $('#pic').attr('width')
// 删除属性
$('#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>Document</title>
<style type="text/css">
.box1 {
background-color: skyblue;
width: 300px;
height: 300px;
position: relative;
}
.box2 {
background-color: red;
width: 100px;
height: 100px;
position: absolute;
top: 50px;
left: 100px;
}
</style>
</head>
<body>
<img src="../img/1.png" width="200" id="pic" title="美女" alt="头像" data-nation="中国">
<div class="box1">
<div class="box2"></div>
</div>
</body>
</html>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
// 跟atrr差不多
// var res = $('#pic').attr('src')
/*
var res = $('img').css('width',200)
var res = $('img').css('box-shadow','0px 0px 5px #888')
下面这个是简介的链式操作,突出个高端
*/
var res = $('img').css({
'width': '200',
'box-shadow':'0px 0px 5px #888'
})
// 查看属性,爱查哪个传哪个
// var res = $('#pic').attr('width')
var res = $('img').css('width')
// 查看元素,返回字符串,转换为int 就可以修改元素
var res = parseInt($('img').css('width'))
res += 50
var res = $('img').css('width',res+'px')
// 下面是width(),height(),offset(),position()方法
// width(),height()修改高宽
var res = $('img').width(200)
var res = $('img').height(200)
//offset()就是获取元素的位置
var res = $('img').offset()
// 这个是获取绝对定位的位置
var res = $('.box2').position()
console.log(res)
</script>点击 "运行实例" 按钮查看在线实例

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