javascript DOM编程艺术 案例之局部图片文本更新

本文介绍了一个简单的局部刷新案例,通过JavaScript实现点击导航项时仅更新页面中特定区域的内容,而无需重新加载整个页面。该技术利用了DOM操作来更改图片源和描述文字。
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>局部刷新</title>
	<style>
	*{
		margin:0;
		padding: 0;
		text-decoration: none;
	}
	.main{
		width: 950px;
		height: 460px;
		background-color: #ccc;
		margin-top: 110px;
		margin-right: auto;
		margin-left: auto;
	}
	.left{
		margin-top: 50px;
		display: inline-block;
		width: 500px;
		height: 390px;
		border:1px solid black;
	}
	.left ul{
		width: 500px;
		height: 40px;
		margin-left: auto;
		margin-right: auto;
	}
	.left ul li{
		list-style: none;
		float: left;
		width: 140px;
		height: 40px;
		line-height: 40px;
		text-align: center;

	}
	.right{
		overflow: hidden;
		margin-left: 50px;
		display: inline-block;
		width: 300px;
		height: 390px;
		border:1px solid black;
	}
	#placehoder{
		width: 300px;
		height: 180px;
		margin-right: auto;
		margin-left: auto;
		margin-top: 50px;
		display: block;
	}
	</style>
</head>
<body>
	<div class="main">
		<div class="left">
			<ul>
				<li>
					<!--  return false 阻止href的默认跳转行为 -->
					<a href="images/11.png"  title="这是风景图1" onclick="showPic(this);return false;">风景1</a>
				</li>
				<li>
					<a href="images/12.png"   title="这变成风景图2了" onclick="showPic(this);return false;">风景2</a>
				</li>
				<li>
					<a href="images/13.png"  title="这是最后的一张风景图3" onclick="showPic(this);return false;">风景3</a>
				</li>
			</ul>
			<img id="placehoder" src="images/11.png" alt="my pictures"/>
		</div>

		<div class="right">
			<!--   一定要注意的地方是,这儿一开始不能为空,否则函数中的firstChild.nodeValue执行不了,return flase
			也就无法执行,最后就是无法阻止默认行为! -->
			<p id="description">这是一系列风景图片</p>
		</div>
	</div>
	<script type="text/javascript">
		function showPic(whichpic){			
			var source=whichpic.getAttribute("href");
			var text=whichpic.getAttribute("title");			
			var textshow=document.getElementById("description");
			var placehoder=document.getElementById("placehoder");
			placehoder.setAttribute("src",source);
			/*还要注意的是,不能直接textshow.nodeValue=text,p中的文本内容是p的子元素!!*/
			textshow.firstChild.nodeValue=text;
		}
	</script>
</body>
</html>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值