<!doctype?html>
<html>
<head>
<meta?charset="UTF-8">
<title>Document</title>
</head>
<style?type="text/css">
#box{
width:?300px;
height:?300px;
}
</style>
<script?type="text/javascript">
window.onload?=?function?()?{
var?ull?=document.getElementById('ull');
var?as?=ull.getElementsByTagName('a');
for?(var?i?=?0;?i?<?as.length;?i++)?{
??as[i].onclick?=function?()?{
?????pic(this);
?????return?false;
??}
?}
}
function?pic(x)?{
var?herfs?=?x.getAttribute('href');
var?imgs?=?document.getElementById('box');
imgs.setAttribute('src',?herfs);
var?ti?=?document.getElementById('ti');
var?text?=?x.getAttribute('title');
ti.innerHTML?=?text;
}
</script>
<body>
<ul?id="ull">
<li><a?>go1</a></li>
<li><a?>go2</a></li>
</ul>
<img?src="http://img.boqiicdn.com/Data/BK/A/1607/25/imagick14651469425193_y.jpg"?alt="pic"?id="box">
<p?id="ti">title</p>
</body>
</html>點擊時調(diào)用pic(this),return false取消跳轉(zhuǎn)。沒問題,書上說優(yōu)化onclick,如下????????????????????as[i].onclick?=function?()?{
if?(pic(this))?{
return?false;
}?else?{
return?true;
}
}我覺也沒問題成立鏈接不跳轉(zhuǎn),不成立跳轉(zhuǎn)??蛇\行和之前的不一樣,變成跳轉(zhuǎn)了,為什么?
onclick優(yōu)化問題
啊啊啊啊123
2016-08-26 20:01:19