博主信息
博文 38
粉丝 0
评论 1
访问量 37105
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
attr()和css()方法的完整用法,以及快捷方法:width(),height(),offset(),position()
1
原创
1096人浏览过

实例

<!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>

运行实例 »

点击 "运行实例" 按钮查看在线实例

webwxgetmsgimg (6).jpg

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
  • 1
    2018-03-16 00:39:40
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

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

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学