菜鸟学习javascript实例教程(5)

2008-02-23 07:41:41来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折


</form>
<p>Try the link before and after you have pressed the button!</p>
</body>

</html>


使连接获得焦点和失去焦点

<html>

<head>
<style type="text/css">
a:active {color:blue}
</style>
<script type="text/javascript">
function getfocus()
{
document.getElementById('w3s').focus()
}

function losefocus()
{
document.getElementById('w3s').blur()
}
</script>
</head>

<body>
<a id="w3s" href="Visit'>http://www.w3schools.com">Visit W3Schools.com</a>
<form>
<input type="button" onclick="getfocus()" value="Get Focus">
<input type="button" onclick="losefocus()" value="Lose Focus">
</form>
</body>

</html>

连接打开的方式


<html>
<body>

<script type="text/javascript">
function linkToAnchor(num)
{
var win2=open("tryjs_anchor2.htm","secondLinkWindow","scrollbars=yes,width=250,height=200")
win2.location.hash=num
}
</script>

<h3>Links and Anchors</h3>
<p>Click on a button to display that anchor in window 2!</p>
<form>
<input type="button" value="0" onClick="linkToAnchor(this.value)">
<input type="button" value="1" onClick="linkToAnchor(this.value)">
<input type="button" value="2" onClick="linkToAnchor(this.value)">
<input type="button" value="3" onClick="linkToAnchor(this.value)">
</form>

</body>
</html>


按钮对象


创建一个按钮

<html>
<head>
<script type="text/javascript">
function show_alert()
{
alert("Hello World!")
document.all("myButton").focus()
}
</script>
</head>

<body>
<form>
<input type="button" value="Click me!" name="myButton" onClick="show_alert()" />
</form>
</body>

</html>


显示按钮的名称

<html>

<body>
<form name="myForm">
The form's name is: <input type="text" name="text1" size="20">
<br /><br />
<input type="button" value="Show the form's name" onClick="this.form.text1.value=this.form.name">
</form>
</body>

</html>


显示表单中各个项的名称

<html>
<head>
<script type="text/javascript">
function showFormElements(theForm)
{
str="Form Elements: "
for (i=0; i<theForm.length; i )
str =" \n " theForm.elements[i].name
alert(str)
}
</script>
</head>

<body>
<form>
First name: <input type="text" name="fname" size="20">
<br />
Last name: <input type="text" name="lname" size="20">
<br /><br />
<input type="button" name="button1"
value="Display name of form elements"
onClick="showFormElements(this.form)">
</form>
</body>

</html>


副选框的选择和取消

<html>

<head>
<script type="text/javascript">
function check()
{
var x=document.forms.myForm
x[0].checked=true
}

function uncheck()
{
var x=document.forms.myForm
x[0].checked=false
}
</script>
</head>

<body>

<form name="myForm">
<input type="checkbox" value="on">
<input type="button" onclick="check()" value="Check Checkbox">
<input type="button" onclick="uncheck()" value="Uncheck Checkbox">
</form>

</body>
</html>


表单中的副选框的选择与取消

<html>

<head>
<script type="text/javascript">
function check()
{
coffee=document.forms[0].coffee
answer=document.forms[0].answer
txt=""
for (i=0;i<coffee.length; i)
{
if (coffee[i].checked)
{
txt=txt coffee[i].value " "
}
}
answer.value="You ordered a coffee with " txt
}
</script>
</head>

<body>
<form>
How would you like your coffee?<br /><br />
<input type="checkbox" name="coffee" value="cream">With cream<br />
<input type="checkbox" name="coffee" value="sugar">With sugar<br />
<br />
<input type="button" name="test" onclick="check()" value="Send order">
<br /><br />
<input type="text" name="answer" size="50">
</form>
</body>

</html>


副选框实现的功能(转换为大写)

<html>
<body>

<script type="text/javascript">
function convertField(field)
{
if (document.form1.convertUpper.checked)
{
field.value=field.value.toUpperCase()
}
}

function convertAllFields()
{
document.form1.fname.value=document.form1.fname.value.toUpperCase()

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:网页制作中表单相关特效整理

下一篇:使用Javascript创建XML文件