《JavaScript学习笔记三》if else及function的使用

《JavaScript学习笔记三》:if else

if else的使用

在JavaScript中if else的使用与在其它语言使用的方法一样,例如:我们想实现这样一个功能:有一个按钮,当点击的时候,如果div是显示的,则隐藏,否则显示出来。

由于比较简单,代码如下:

<!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=gb2312" />
    <title>无标题文档</title>
    </head>
    <style>
    #div1 {width:200px;height:100px;background:red;border:1px solid:#999;display:none}
    </style>
    <script>
    function moreContent()
    {
        var oDiv = document.getElementById('div1');
        //如果此时是显示的状态,则就消失,否则显示。
        if(oDiv.style.display=="block")
        {
            oDiv.style.display="none";
        }
        else
        {
            oDiv.style.display="block";
        }
    }
    </script>
    <body>
    <input type="button" value="更多" onclick="moreContent()" />

    <div id="div1">
    这里有更多内容哟
    </div>
    </body>
    </html>

更改样式

上面的例子程序都是只是改变一个属性的值,下面我们就来尝试有一个函数来完成对不同属性的值的改变。

现在我们想实现这样的功能

有三个按钮,一个按钮改变div的宽度,一个按钮改变div的长度,一个按钮改变div的颜色,应该如何来实现呢??

这个功能比较简单,你可以选择这样实现:直接在标签里面写代码:

<!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=gb2312" />
    <title>无标题文档</title>
    </head>

    <style>
    #div1 {width:200px;height:100px;border:1px solid:#999;background:red;display:block;}
    </style>


    <body>
    <input type="button" value="改变宽度"  onclick="document.getElementById('div1').style.width='400px';" />  <!-- 比较不懂的是为什么 width/height 的赋值都是字符串 -->
    <input type="button" value="改变长度"  onclick="document.getElementById('div1').style.height='300px';"/>
    <input type="button" value="改变颜色"  onclick="document.getElementById('div1').style.background='black';"/>
    <div id="div1">

    </div>
    </body>
    </html>

你也可以选择写3个函数来完成:

    <!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=gb2312" />
    <title>无标题文档</title>
    </head>

    <style>
    #div1 {width:200px;height:100px;border:1px solid:#999;background:red;display:block;}
    </style>
    <script>
    function changeWidth(content)
    {
        var oDiv=document.getElementById('div1');
        oDiv.style.width=content;
    }
    function changeHeight(content)
    {
        var oDiv=document.getElementById('div1');
        oDiv.style.height=content;
    }
    function changeColor(content)
    {
        var oDiv=document.getElementById('div1');
        oDiv.style.background=content;
    }
    </script>

    <body>
    <input type="button" value="改变宽度"  onclick="changeWidth('400px')" /> 
    <input type="button" value="改变长度"  onclick="changeHeight('300px')"/>
    <input type="button" value="改变颜色"  onclick="changeColor('black')"/>
    <div id="div1">

    </div>
    </body>
    </html>

你也可以写一个函数来完成:

    <!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=gb2312" />
    <title>无标题文档</title>
    </head>

    <style>
    #div1 {width:200px;height:100px;border:1px solid:#999;background:red;display:block;}
    </style>
    <script>
    function setStyle(name,value)
    {
        var oDiv = document.getElementById("div1");
        //oDiv.style[name]=value;//注意:这里就不能使用oDiv.style.name=value;
    }
    </script>

    <body>
    <input type="button" value="改变宽度"  onclick="setStyle('width','400px')" />     <!--  注意:这里传进去的参数均为字符串-->
    <input type="button" value="改变长度"  onclick="setStyle('height','300px')"/>
    <input type="button" value="改变颜色"  onclick="setStyle('background','black')"/>
    <div id="div1">

    </div>
    </body>
    </html>

小结

最近在学习JavaScript,这三篇《JavaScript学习笔记三》所举的例子都比较简单, 主要是用来练手,熟悉JavaScript,接下来会继续学习更多的东西。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值