GetElementById方法(非常常用),根据元素Id获得对象,网页中Id不能重复,也可以直接通过元素的Id来引用元素,但是有有效范围之类的问题,因此不建议直接通过Id操作元素,而是通过GetElementById .
GetElementById方法在VisualStudio2010中不能在Form中编写,否则有错误。
<head>
<title></title>
<script type ="text/javascript">
functionbtnClick() {
// alert(textbox.value);
//还可¦以样写不管¨在不在Form中D都可用
vartxt = document.getElementById(textbox);
alert(textbox.value);
}
functionbtnClickboss() {
// alert(textboss.value);//错䨪误¨®
// alert(form1.textboss.value);//正y确¨¡¤
vartxt = document.getElementById(textboss);
alert(textboss.value);
}
</script>
</head>
<body>
<input type ="text" id ="textbox" />
<input type ="button"value="press" onclick="btnClick()" />
<!--<formaction="HTMLPage新?闻?代䨲码?1.htm"id ="form1">
<inputtype ="text" id ="textboss" />
<inputtype ="button" value ="whatever you write anything to out"onclick ="btnClickboss()" />
</form>-->
<input type ="text" id ="textboss" />
<input type ="button" value="whatever you write anything to out"onclick ="btnClickboss()"/>
</body>
</html>
GetElementName,根据元素的Name获得对象,由于页面中元素的Name可以重复,比如多个RadioButton的Name一样,因此GetElementByName返回值是对象的数组。
<head>
<title></title>
<script type ="text/javascript" >
var radios= document.getElementsByName("gender");
functionbtnClick() {
// //在¨²JS中Dfor(var radio in radios)不?像?C#中D的Ì?foreach遍À¨¦历¤¨²每?个?元a素?,ê?而?是º?遍À¨¦历¤¨²的Ì?Key(ê¡§属º?性?)ê?
// for (var radio in radios) {
// alert(radio.value);
// }
for(i = 0; i < radios.length; i++) {
varradio = radios[i];
alert(radio.value);
}
}
</script>
</head>
<body>
<input type="radio"name="gender"value="boy" />boy
<input type ="radio"name="gender"value="girl" />girl
<input type ="radio"name= "gender"value="protect"/>mimi
<input type ="button"value="click"onclick="btnClick()" />
</body>
</html>
<head>
<title></title>
<style type="text/css"> div{font-size:12px;font-family:arial}.baidu{font-size:14px;line-height:24px;font-family:arial} a,a:link{color:#0000cc;}
.baidu span{color:#6f6f6f;font-size:12px} a.more{color:#008000;}a.blk{color:#000;font-weight:bold;}</style>
</head>
<body>
林依晨的资料
<script language="JavaScript" type="text/JavaScript" src="http://news.baidu.com/ns?word=title%3A%C1%D6%D2%C0%B3%BF&tn=newsfcu&from=news&cl=2&rn=5&ct=0"></script>
依晨好靓
</body>
睡前必听:SHE花都开好了,林依晨孤单北半球,少女时代Gee。今天又完了。。。