js

<body>
该文档的标题是:
<script type="text/javascript">
var dd=document;
    dd.write(document.body)
</script>
该文档的URl是:
<script type="text/javascript">
var dd=document;
    dd.write(document.URL)
</script>
该文档的域名是:
<script type="text/javascript">
var dd=document;
    dd.write(document.domain)
</script>
文档中锚的数目:
<script type="text/javascript">
document.write(document.anchors.length)
</script>
<script type="text/javascript">
document.write("文档包含: " + document.forms.length + " 个表单。")
</script>
本文档中第一个锚的 InnerHTML 是:
<script type="text/javascript">
document.write(document.anchors[0].innerHTML)
</script>
<script type="text/javascript">
document.write("本文档包含:" + document.images.length + " 幅图像。")
</script>
得到提交表单的方法
function showMethod()
  {
  var x=document.getElementById("myForm")
  alert(x.method)
  }
</script>
<script type="text/javascript">
function formSubmit()
{
document.getElementById("myForm").submit()
}
</script>
===================================
<html>
<head>
<script type="text/javascript">
function getValue()
{
var x=document.getElementById("myHeader")
alert(x.innerHTML)
}
</script>
</head>
<body>
<h1 id="myHeader" onclick="getValue()">liuchao</h1>
<p>点击标题,会提示出它的值。</p>
</body>
</html>
========================================
<html>
<head>
<script type="text/javascript">
function check()
{
document.getElementById("myCheck").checked=true
}

function uncheck()
{
document.getElementById("myCheck").checked=false
}
</script>
</head>

<body>
<form>
<input type="checkbox" id="myCheck" />
<input type="button" onclick="check()" value="选定复选框" />
<input type="button" onclick="uncheck()" value="取消选定复选框" />
</form>
</body>
</html>
==========================================
<html>
<head>
<script type="text/javascript">
function createOrder()
{
coffee=document.forms[0].coffee
txt=""
for (i=0;i<coffee.length;++ i)
{
if (coffee[i].checked)
{
txt=txt + coffee[i].value + " "
}
}
document.getElementById("order").value="您订购的咖啡带有: " + txt
}
</script>
</head>
<body>
<p>你喜欢怎么喝咖啡?</p>
<form>
<input type="checkbox" name="coffee" value="奶油">加奶油<br />
<input type="checkbox" name="coffee" value="糖块">加糖块<br />
<br />
<input type="button" onclick="createOrder()" value="发送订单">
<br /><br />
<input type="text" id="order" size="50">
</form>
</body>
</html>
==================================================
<html>
<head>
<script type="text/javascript">
function convertToUcase()
{
document.getElementById("fname").value=document.getElementById("fname").value.toUpperCase()
document.getElementById("lname").value=document.getElementById("lname").value.toUpperCase()
//document.getElementById("lname").value=document.getElementById("lname").value.toLowerCase()
}
</script>
</head>

<body>
<form name="form1">
名:<input type="text" id="fname" size="20" />
<br /><br />
姓:<input type="text" id="lname" size="20" />
<br /><br />
转换为大写
<input type="checkBox" onclick="if (this.checked) {convertToUcase()}">
</form>
</body>
</html>
===================================================
<html>
<head>
<script type="text/javascript">
function check(browser)
  {
  document.getElementById("answer").value=browser
  }
</script>
</head>
<body>
<p>您喜欢哪款浏览器?</p>
<form>
<input type="radio" name="browser" onclick="check(this.value)" value="Internet Explorer">Internet Explorer<br />
<input type="radio" name="browser" onclick="check(this.value)" value="Firefox">Firefox<br />
<input type="radio" name="browser" onclick="check(this.value)" value="Netscape">Netscape<br />
<input type="radio" name="browser" onclick="check(this.value)" value="Opera">Opera<br />
<br />
您喜欢的浏览器是:<input type="text" id="answer" size="20">
</form>
</body>
</html>
==========================================
表单验证
<html>
<head>
<script type="text/javascript">
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
 }
}
</script>
</head>
<body>
<form action="/example/hdom/hdom_submitpage.html" onsubmit="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>
</body>
</html>
=========================================================
选定某段文本
<html>
<head>
<script type="text/javascript">
function selText()
{
document.getElementById("myText").select()
}
</script>
</head>

<body>
<form>
<input size="25" type="text" id="myText" value="选定我吧!">
<input type="button" value="选择文本" onclick="selText()">
</form>
</body>
</html>
================================================================
用文字来代替“提交”按钮
<form name="form" action="">
<a href="javascript:document.form.submit()">提交</a>
</form>
<a href="javascript:void(0);" onclick="doSubmit()">Test </a>

<script language="javascript">
  function doSubmit(){
      window.document.forms[0].action="";//如果页面有多个表单的话 你算清楚你那个表单是第几个
      window.document.forms[0].submit();
  }
</script>

你可能感兴趣的:(JavaScript,function,文档,input,button,browser)