您的位置:首页 > Web前端 > JavaScript

JS练习(个人练习有许多问题,解决不了)

2016-03-03 20:03 513 查看
[b]初入javascript[/b]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
#but{
width:80px;
height:30px;
border:0;
background:#F00;
font-size:13px;
font-weight:bold;
color:#FFF;
}
button{
width:40px;
height:40px;
background:#CCC;
border:1px #666666 solid;
}
button:hover{
background:#C90;
}
#but1:hover{
background:#F00;
border:1px #000000 solid;
}
#but2:hover{
background:#FF0;
border:1px #000000 solid;
}
#but3:hover{
background:#00F;
border:1px #000000 solid;
}
#div1{
width:150px;
height:150px;
border:3px #333333 solid;
}
#div2{
width:300px;
height:250px;
margin-left:auto;
margin-right:auto;
border:1px solid;
text-align:center;
display:none;
}
#but10{
width:60px;
height:30px;
background:#00C;
}
#but11{
width:60px;
height:30px;
background:#00C;
}
</style>
<script>
window.onload = function (){
var odiv1 = document.getElementById('div1');
var odiv2 = document.getElementById('div2');

document.getElementById('but').onmousedown = function (){
this.style.color = 'black';
};
document.getElementById('but').onclick = function (){
odiv2.style.display = 'block';
};
document.getElementById('but1').onclick = function(){
odiv1.style.background = 'red';
};
document.getElementById('but2').onclick = function(){
odiv1.style.background = 'yellow';
};
document.getElementById('but3').onclick = function(){
odiv1.style.background = 'blue';
};
document.getElementById('but4').onclick = function(){
odiv1.style.width = '200px';
};
document.getElementById('but5').onclick = function(){
odiv1.style.width = '300px';
};
document.getElementById('but6').onclick = function(){
odiv1.style.width = '400px';
};
document.getElementById('but7').onclick = function(){
odiv1.style.height = '200px';
};
document.getElementById('but8').onclick = function(){
odiv1.style.height = '300px';
}; document.getElementById('but9').onclick = function(){
odiv1.style.height = '400px';
};
document.getElementById('but10').onclick = function (){
odiv1.style.width = '150px';
odiv1.style.height = '150px';
odiv1.style.border = '3px #333333 solid';
odiv1.style.background = '#FFF';
};
document.getElementById('but11').onclick = function (){
odiv2.style.display= 'none';
};

};
</script>
</head>

<body>
请为以下div设置样式:
<button id="but">点击设置</button>
<div id="div1"></div>
<div id="div2">
<div style="height:10px;"></div>
<div>请选择北京颜色:<button id="but1">红</button> <button id="but2">黄</button> <button id="but3">蓝</button></div>
<div style="height:7px;"></div>
<div>请选择宽(px):<button id="but4">200</button> <button id="but5">300</button> <button id="but6">400</button></div>
<div style="height:7px;"></div>
<div>请选择高(px):<button id="but7">200</button> <button id="but8">300</button> <button id="but9">400</button></div>
<div style="height:10px;"></div>
<div><button id="but10">恢复</button>    <button id="but11">确定</button></div>
</div>
</body>
</html>

[b]聊天模拟器[/b]

/*

所用图片




*/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
#div1{
width:240px;
height:400px;
word-wrap:break-word;
overflow:auto;
border:1px solid #666;
}
</style>
<script>
window.onload = function(){
var oText = document.getElementById('text1');
var oBut1 = document.getElementById('but1');
var oDiv = document.getElementById('div1');
var oImg = document.getElementById('img1');
var oClick = true;
oImg.onclick = function(){
if(oClick){
oImg.src = 'img/XLVSTFW4)]P63[TBZ}(RYR5.png';
oClick = false;
}else{
oImg.src = 'img/E8B4`LK}CKXMU9F`B6{K_HH.png';
oClick = true;
};
};
oBut1.onclick = function(){
if(oText.value==''){
alert('请输入内容!');
}else{
oDiv.innerHTML = oImg + oText.value + '<br/>' + oDiv.innerHTML;
oText.value = '';
};
};
};
</script>
</head>

<body>
<div id="div1"></div>
<img id="img1" src="img/E8B4`LK}CKXMU9F`B6{K_HH.png" />
<input id="text1" type="text" value="" />
<input id="but1" type="button" value="发送" />
</body>
</html>

[b]图片切换[/b]

/*

所用图片








*/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
p { margin:0; }
body { text-align:center; }
#box { width:400px; height:400px; border:10px solid #ccc; margin:50px auto 0; position:relative; }
a { width:40px; height:40px; background:#fff; filter:alpha(opacity:80); opacity:0.8; position:absolute; top:160px; font-size:18px; color:#000; text-align:center; line-height:40px; text-decoration:none; }
a:hover { filter:alpha(opacity:30); opacity:0.3; }
#prev { left:10px; }
#next { right:10px; }
#p1 { width:400px; height:30px; line-height:30px; text-align:center; background:#000; color:#fff; font-size:14px; filter:alpha(opacity:80); opacity:0.8; position:absolute; bottom:0; left:0; }
strong { width:400px; height:30px; line-height:30px; text-align:center; background:#000; color:#fff; font-size:14px; filter:alpha(opacity:80); opacity:0.8; position:absolute; top:0; left:0; }
#img1 { width:400px; height:400px; }
span { position:absolute; width:400px; height:30px; line-height:30px; text-align:center; top:-50px; left:0; font-family:'微软雅黑'; }
</style>
<script>
window.onload = function(){
var oBut1=document.getElementById('but1');
var oBut2=document.getElementById('but2');
var oPrev=document.getElementById('prev');
var oNext=document.getElementById('next');
var oP=document.getElementById('p1');
var oStrong=document.getElementById('strong1');
var oImg=document.getElementById('img1');
var arrUrl = ['image/1.jpg','image/2.jpg','image/3.jpg','image/4.jpg'];
var arrText = ['文字一','文字二','文字三','文字四'];
var num = 0;
oNext.onclick=function(){
num=num+1;
oP.innerHTML=arrText[num-1];
oStrong.innerHTML=num+'/'+ arrUrl.length;
oImg.src=arrUrl[num-1];
if(num==arrUrl.length){
num=0;
};
};
oPrev.onclick=function(){
num--;
if(num==-1){
num=arrUrl.length-1;
};
oP.innerHTML=arrText[num];
oStrong.innerHTML=num+1 +'/'+ arrUrl.length;
oImg.src=arrUrl[num];
};

};
</script>
</head>

<body>
<input id="but1" type="button" value="循环切换" />
<input id="but2" type="button" value="顺序切换" />
<div id="box"><span>图片可从最后一张跳转到第一张循环切换</span>
<a id="prev" href="javascript:;"><</a>
<a id="next" href="javascript:;">></a>
<p id="p1">图片文字加载中……</p>
<strong id="strong1">图片数量计算中……</strong>
<img id="img1" />
</div>
</body>
</html>

[b]文档放大缩小[/b]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
.red{
width:250px;
height:1000px;
background:red;
color:yellow;
border:1px solid #666;
}
.yellow{
width:250px;
height:1000px;
background:yellow;
color:red;
border:1px solid #666;
}
</style>
<script>
window.onload = function(){
var oBut1 = document.getElementById('but1');
var oBut2 = document.getElementById('but2');
var oBut3 = document.getElementById('but3');
var oBut4 = document.getElementById('but4');
var oP = document.getElementById('p1');
var num = 12;
oBut1.onclick = function(){
/*num--;
oP.style.fontSize = num +'px';*/
if(num>10){
num--;
oP.style.fontSize = num +'px';
};
};
oBut2.onclick = function(){
num++;
oP.style.fontSize = num + 'px';
};
oBut3.onclick = function(){
oP.className = 'red';
};
oBut4.onclick = function(){
oP.className = 'yellow';
};
};
</script>
</head>

<body>
<input id="but1" type="button" value="-" />
<input id="but2" type="button" value="+" />
<input id="but3" type="button" value="红" />
<input id="but4" type="button" value="黄" />
<pre>
<p id="p1">
唐人
词曲:孙子涵
演唱:孙子涵
一如昨日烛火 伴扁舟相随
哪有唐人不懂得陶醉
我孤舟 你窈窕 岸上有隐晦
一踏万里与谁相随
你穿错了嫁妆怎能有快乐
再上一层胭脂也不美
一声戛然而止庭前的鞭炮
妄想同你华发的心作废
你说不要自作自受自己创造伤悲
谁都可以彻底忘记谁
你说过往不及回首 别后悔了才会
想方设法的把你追回
你说孤独是诗人应该具有的体会
写歌的人就该有伤悲
我点一丝烛火 一时泛滥了思念
写首小调名字叫 后悔
一如昨日烛火 伴着扁舟相随
哪有唐人不懂陶醉
你穿错了嫁妆怎能有快乐
再上一层胭脂也不美
你穿错了嫁妆怎可能有快乐
再上一层胭脂也不美
一声戛然而止庭前的鞭炮
妄想同你华发的心作废
你说不要自作自受自己创造伤悲
谁都可以彻底忘记谁
你说过往不及回首 别后悔了才会
想方设法得把你追回
你说孤独是诗人应该具有的体会
写歌的人就该有伤悲
我点一丝烛火 一时泛滥了思念
写首小调名字叫 后悔
你说不要自作自受自己创造伤悲
谁都可以彻底忘记谁
你说过往不及回首 别后悔了才会
想方设法的把你追回
你说孤独是诗人应该具有的体会
写歌的人就应该有伤悲
我点一丝烛火 一时泛滥了思念
写首小调名字叫 后悔
一如昨日烛火 伴着扁舟相随
哪有唐人不懂陶醉
你穿错了嫁妆怎可能有快乐
再上一层胭脂也不美
一如昨日烛火 伴着扁舟相随
哪有唐人不懂陶醉
你穿错了嫁妆怎能有快乐
再上一层胭脂也不美
</p>
</pre>
</body>
</html>

[b]自动生成10个DIV(用定位实现)[/b]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
div{width:50px;
height:50px;
text-align:center;
line-height:50px;
position:absolute;
border:1px #FF0000 solid;
}
</style>
<script>
window.onload=function(){
var aDiv=document.getElementsByTagName('div');
var oBut=document.getElementById('but1');
var arr=['red','yellow','blue','green'];
oBut.onclick=function(){
for(var i=0;i<10;i++){
document.body.innerHTML+='<div>' +i+'</div>';
};
for(var i=0;i<aDiv.length;i++){
aDiv[i].style.left=10+i*60+'px';
};
for(var i=0;i<aDiv.length;i++){
aDiv[i].style.background=arr[i%arr.length]; //取模
};
};
};
</script>
</head>

<body>
<input id="but1" type="button" value="自动生成10个DIV" />
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: