js语法学习

<!DOCTYPE html>
<html>

<head>
<!-- head中的js -->
<script src="jsfile.js">
function changeImage()
{
element=document.getElementById('myimage')
if (element.src.match("me"))
  {
  element.src="id.jpeg";
  }
else
  {
  element.src="me.jpg";
  }
}

function myFunction()
{
x=document.getElementById("demo") // 找到元素
var color= x.style.color;
// x.style.color="#ffff00"; 
if(x.style.color == "#ffff00")       // 改变样式}
{
    x.style.color="ff00ff";
}else {
    x.style.color="ffff00";
}
}

function aaa(){
    var txt = "Hello";
    var x=document.getElementById("aaaa"); // 找到元素
    document.write("txt.length=" + x.innerHTML.length);
}
</script>
</head>


<body>
<!-- <body οnkeyup="whichButton(event)" οnmοusedοwn="whichElement(event)"> -->
<p>
JavaScript 能够直接写入 HTML 输出流中:
</p>

<script>
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph.</p>");
</script>

<p>
您只能在 HTML 输出流中使用 <strong>document.write</strong>。
如果您在文档已加载后使用它(比如在函数中),会覆盖整个文档。
</p>

<!-- <img id="myimage" οnclick="changeImage()" src="me.jpg"> -->
<p>点击灯泡来点亮或熄灭这盏灯</p>

<p id="demo" color:red>
JavaScript 能改变 HTML 元素的样式。
</p>
<button type="button" οnclick="myFunction()">点击这里</button>
<button type="button" οnclick="aaa()">点击这里</button>

<p id="aaaa" color:red>
JavaScript 能改变 HTML 元素的样式。
</p>

<p id="bb">A Paragraph.</p>
<button type="button" οnclick="bb()">点击这里</button>
<p><b>注释:</b>myFunction 保存在名为 "myScript.js" 的外部文件中。</p>

<!-- try catch -->
<p>请输入 5 到 10 之间的数字:</p>
<input id="trycatch" type="text">
<button type="button" οnclick="trycatch()">测试输入值</button>
<p id="mess"></p>

<h1 id="id1">My Heading 1</h1>
<button type="button" 
οnclick="document.getElementById('id1').style.color='red'">
点击这里!</button>

<form name="Form1"></form>
<form name="Form2"></form>
<form name="Form3"></form>

<!-- 带入radio的选择值到输入框 -->
<form id="myForm">
<input type="radio" name="browser" οnclick="check(this.value)" value="Internet Explorer">Internet Explorer<br />
<input type="radio" name="browser" οnclick="check(this.value)" value="Firefox">Firefox<br />
<input type="radio" name="browser" οnclick="check(this.value)" value="Netscape">Netscape<br />
<input type="radio" name="browser" οnclick="check(this.value)" value="Opera">Opera<br />
<br />
您喜欢的浏览器是:<input type="text" id="answer" size="20">
</br>
<input type="button" οnclick="formReset()" value="重置">
</form>

<form action="/example/hdom/hdom_submitpage.html" οnsubmit="return validate()">
名字(最多 10 个字符):<input type="text" id="fname" size="20"><br />
年龄(从 1 到 100):<input type="text" id="age" size="20"><br />
电子邮件:<input type="text" id="email" size="20"><br />
<br />
<input type="submit" value="提交"> 
</form>


<form>
请选择您喜欢的浏览器:
<select id="myList" οnchange="favBrowser()">
  <option>Internet Explorer</option>
  <option>Netscape</option>
  <option>Opera</option>
</select>
<p>您喜欢的浏览器是:<input type="text" id="favorite" size="20"></p>
</form>


<form id="myForm1">
<input size="3" tabindex="1" maxlength="1" οnkeyup="checkLen(this,this.value)">
<input size="2" tabindex="2" maxlength="1" οnkeyup="checkLen(this,this.value)">
<input size="3" tabindex="3" maxlength="1" οnkeyup="checkLen(this,this.value)">
</form>

<iframe src="/example/hdom/frame_a.html" id="frame1"></iframe>
<iframe src="/example/hdom/frame_b.html" id="frame2"></iframe>
<br /><br />
<input type="button" οnclick="changeSource()" value="改变两个框架的 source">


<form>
<input type="button" value="开始计时!" onClick="timedCount()">
<input type="text" id="txt">
</form>

<!-- body中的js -->
<script>
// for循环
var i;
var cars = new Array();
cars[0] = "Audi";
cars[1] = "BMW";
cars[2] = "Volvo";

for (i=0;i<cars.length;i++)
{
document.write(cars[i] + "<br>");
}
var visitor="PRES";
// 三元判断
greeting=(visitor=="PRES")?"Dear President ":"Dear ";
document.write(greeting);

// 判断
document.write("</br>");

var x="";
var time=new Date().getHours();
if (time<20)
  {
  x="Good day";
  }
else
  {
  x="Good evening";
  }
document.write(x);

// switch case
document.write("</br>");

var day=new Date().getDay();
switch (day)
{
case 0:
  x="Today it's Sunday";
  break;
case 1:
  x="Today it's Monday";
  break;
case 2:
  x="Today it's Tuesday";
  break;
case 3:
  x="Today it's Wednesday";
  break;
case 4:
  x="Today it's Thursday";
  break;
case 5:
  x="Today it's Friday";
  break;
case 6:
  x="Today it's Saturday";
  break;
default:
  x="Today it's Saturday";
  break;
}

document.write(x);

document.write("</br>");
// while 循环
cars=["BMW","Volvo","Saab","Ford"];
var i=0;
while (cars[i])
{
document.write(cars[i] + "<br>");
i++;
}


function trycatch()
{
try

var x=document.getElementById("trycatch").value;
if(x=="")    throw "值为空";
if(isNaN(x)) throw "不是数字";
if(x>10)     throw "太大";
if(x<5)      throw "太小";
}
catch(err)
{
var y=document.getElementById("mess");
y.innerHTML="错误:" + err + "。";
}
}

document.write("文档包含: " + document.forms.length + " 个表单。");
document.write(document.domain);
document.write(document.anchors.length);

function whichButton(event)
{
alert(event.keyCode)
}

function whichElement(e)
{
var targ
if (!e) var e = window.event
if (e.target) targ = e.target
else if (e.srcElement) targ = e.srcElement
if (targ.nodeType == 3) // defeat Safari bug
   targ = targ.parentNode
var tname
tname=targ.tagName
alert("You clicked on a " + tname + " element.")
}

function whichElement(e)
{
/*var targ
var e = window.event
if (e.target) targ = e.target
else if (e.srcElement) targ = e.srcElement
var tname
tname=targ.tagName
alert("You clicked on a " + tname + " element.")*/
}


function check(browser)
  {
  document.getElementById("answer").value=browser
  }

  function formReset()
{
document.getElementById("myForm").reset()
}


function validate()
{
var at=document.getElementById("email").value.indexOf("@")
var age=document.getElementById("age").value
var fname=document.getElementById("fname").value
submitOK="true"

if (fname.length>10)
 {
 alert("名字必须小于 10 个字符。")
 submitOK="false"
 }
if (isNaN(age)||age<1||age>100)
 {
 alert("年龄必须是 1 与 100 之间的数字。")
 submitOK="false"
 }
if (at==-1) 
 {
 alert("不是有效的电子邮件地址。")
 submitOK="false"
 }
if (submitOK=="false")
 {
 return false
 }
}


function favBrowser()
{
var mylist=document.getElementById("myList")
document.getElementById("favorite").value=mylist.options[mylist.selectedIndex].text
}

function checkLen(x,y)
{
if (y.length==x.maxLength)
    {
    var next=x.tabIndex
    if (next<document.getElementById("myForm1").length)
        {
        document.getElementById("myForm1").elements[next].focus()
        }
    }
}

function changeSource()
{
document.getElementById("frame1").src="http://www.baidu.com"
document.getElementById("frame2").src="http:www.163.com"
}

// <!-- 检测有关客户机的屏幕的细节 -->
document.write("<br />")
document.write("Screen resolution: ")
document.write(screen.width + "*" + screen.height)
document.write("<br />")
document.write("Available view area: ")
document.write(screen.availWidth + "*" + screen.availHeight)
document.write("<br />")
document.write("Color depth: ")
document.write(screen.colorDepth)
document.write("<br />")
document.write("Buffer depth: ")
document.write(screen.bufferDepth)
document.write("<br />")
document.write("DeviceXDPI: ")
document.write(screen.deviceXDPI)
document.write("<br />")
document.write("DeviceYDPI: ")
document.write(screen.deviceYDPI)
document.write("<br />")
document.write("LogicalXDPI: ")
document.write(screen.logicalXDPI)
document.write("<br />")
document.write("LogicalYDPI: ")
document.write(screen.logicalYDPI)
document.write("<br />")
document.write("FontSmoothingEnabled: ")
document.write(screen.fontSmoothingEnabled)
document.write("<br />")
document.write("PixelDepth: ")
document.write(screen.pixelDepth)
document.write("<br />")
document.write("UpdateInterval: ")
document.write(screen.updateInterval)
document.write("<br />")


var c=0
var t
function timedCount()
{
if(c == 11){
document.getElementById('txt').value="重新开始";
return;
}
document.getElementById('txt').value=c
c=c+1
t=setTimeout("timedCount()",1000)
}

</script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值