博主信息
博文 24
粉丝 0
评论 1
访问量 18270
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
0404课后作业
张成钢的博客
原创
937人浏览过

实例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>attr()和CSS()方法</title>
	<style type="text/css">
		.box1 {
			width: 1000px;
			height: 800px;
			margin-left: 5px;
			/*background-color: wheat;*/
			margin: 10px;
			position: relative;

		}

		
		.divBtn {
			margin-top: 50px;
		}

		button {
			width: 100px;
			height: 50px;
			background-color: skyblue;
			color: red;
			text-align: center;
			border-style: none;
		}

		img{
			width: 150px;
			height: 150px;
			margin-top: 15px;

		}
	</style>


</head>
<body>
	<div class="box1">


	<img src="../images/jsy.jpg">

	<div id="divBtn">
		<button type="button" id="btnCSS">CSS()读写</button>
		<button type="button" id="btnAttr">attr()读写</button>
		<button type="button" id="btnW">width()方法</button>
		<button type="button" id="btnH">height()方法</button>
		<button type="button" id="btnPos">position()方法</button>
		<button type="button" id="btnOffset">offset()方法</button>
		<button type="button" id="btnDef">默认值</button>
	</div>
	</div>	


</body>
</html>

<!-- 1.在线引用 -->
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<!-- <script type="text/javascript" src="../js/jquery-3.3.1.js"></script> -->

<script type="text/javascript">
	//1.Css方法设置样式
	$("#btnCSS").click(function(){
		$("img").css('width','200')
		$("img").css('height','200')
		$("img").css('border-radius','10%')
		$("img").css('box-shadow','3px 3px 3px #888')

		//简写
		// $("img").css({
		// 	'width':'200',
		// 	'height':'200',
		// 	'border-radius':'10%',
		// 	'box-shadow': '2px 2px 2px #888'
		// })

		alert('CSS方法:图片width=' + $("img").css('width'))
	})

	//2. attr 方法
	$("#btnAttr").click(function(){
		$("img").attr('src','../images/gyy.jpg')
		$("img").attr('style','border-radius:20%;box-shadow:3px 3px 3px #666;width:200px;height:200px')
		// $("img").removeAttr('style')
		alert('attr方法:图片src=' + $("img").attr('src'))
	})

	//3.width()方法读写
	$("#btnW").click(function(){
		$("img").width('+=50')
		alert('width()方法:图片width=' + $("img").width())
	})

	//4.height()方法读写
	$("#btnH").click(function(){
		$("img").height('+=50')
		alert('height()方法:图片height=' + $("img").height())
	})	

	//5.position()方法
	$("#btnPos").click(function(){
		var x = $("#btnCSS").position()
		alert('CSS()读写按钮position.left=' + x.left + ';' + 'top=' + x.top)
	})
		
	//6.Offset()方法
	$("#btnOffset").click(function(){
		var y = $("img").offset()
		alert('图片Offset.left=' + y.left + ';' + '图片top=' + y.top)
	})

	//7. 还原初始照片
	$("#btnDef").click(function(){
		$("img").attr('src','../images/jsy.jpg')
		$("img").removeAttr('style');

	})

</script>

运行实例 »

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

0404.png

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

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

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