高级 JavaScript 实例02

创建一个欢迎 cookie
<html>
<head>
<script type="text/javascript">
function getCookie(c_name)
{
if (document.cookie.length>0)
{ 
c_start=document.cookie.indexOf(c_name + "=")
if (c_start!=-1)
{ 
c_start=c_start + c_name.length+1 
c_end=document.cookie.indexOf(";",c_start)
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end))
} 
}
return ""
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : "; expires="+exdate.toGMTString())
}

function checkCookie()
{
username=getCookie('username')
if (username!=null && username!="")
  {alert('Welcome again '+username+'!')}
else 
  {
  username=prompt('Please enter your name:',"")
  if (username!=null && username!="")
    {
    setCookie('username',username,365)
    }
  }
}
</script>
</head>
<body onLoad="checkCookie()">
</body>
</html>

按钮动画
<html>
<head>
<script type="text/javascript">
function mouseOver()
{
document.b1.src ="../i/eg_mouse.jpg"/*tpa=http://www.w3school.com.cn/i/eg_mouse.jpg*/
}
function mouseOut()
{
document.b1.src ="../i/eg_mouse2.jpg"/*tpa=http://www.w3school.com.cn/i/eg_mouse2.jpg*/
}
</script>
</head>

<body>
<a href="../index.html" tppabs="http://www.w3school.com.cn/index.html" target="_blank">
<img border="0" alt="Visit W3School!" src="../i/eg_mouse2.jpg" tppabs="http://www.w3school.com.cn/i/eg_mouse2.jpg" name="b1"  onmouseover="mouseOver()" onmouseout="mouseOut()" /></a>
</body>
</html>

添加了 JavaScript 的图像映射
<html>
<head>
<script type="text/javascript">
function writeText(txt)
{
document.getElementById("desc").innerHTML=txt
}
</script>
</head>

<body>
<img src="../i/eg_planets.jpg" tppabs="http://www.w3school.com.cn/i/eg_planets.jpg" border="0" usemap="#planetmap" alt="Planets" />

<map name="planetmap" id="planetmap">

<area shape="circle" coords="180,139,14"
onMouseOver="writeText('直到 20 世纪 60 年代,金星一直被认为是地球的孪生姐妹,因为金星是离我们最近的行星,同时还由于两者拥有很多共同的特征。')"
href ="../example/html/venus.html" tppabs="http://www.w3school.com.cn/example/html/venus.html" target ="_blank" alt="Venus" />

<area shape="circle" coords="129,161,10"
onMouseOver="writeText('从地球上是很难研究水星的,这是由于它和太阳的距离总是很近。')"
href ="../example/html/mercur.html" tppabs="http://www.w3school.com.cn/example/html/mercur.html" target ="_blank" alt="Mercury" />

<area shape="rect" coords="0,0,110,260"
onMouseOver="writeText('太阳和类似木星这样的气态行星是到目前为止太阳系中最大的物体。')"
href ="../example/html/sun.html" tppabs="http://www.w3school.com.cn/example/html/sun.html" target ="_blank" alt="Sun" />

</map>

<p id="desc"></p>

</body>
</html>

转载于:https://my.oschina.net/u/2552902/blog/543859

猜你喜欢

转载自blog.csdn.net/weixin_34342578/article/details/92326394