【JS學習筆記】函數傳參

來源:互聯網
上載者:User

標籤:1.0   oct   ack   xhtml   height   單引號   函數傳參   傳遞   etc   

比如僅僅改變背景的顏色

  函數傳參:參數就是預留位置。

  那麼在什麼時候用傳參呢?函數裡定不下來的東西。

<!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=utf-8" />
<title>無標題文檔</title>
<style>
    #div1{
        width:200px;
        height:200px;
        background:red;
        }
</style>
<script>
    function setColor(color)
    {
        var oDiv=document.getElementById(‘div1‘);
        oDiv.style.background=color;
        }
    /*function toGreen()
    {
        var oDiv=document.getElementById(‘div1‘);
        oDiv.style.background=‘green‘;
        }
    function toYellow()
    {
        var oDiv=document.getElementById(‘div1‘);
        oDiv.style.background=‘yellow‘;
        }
    function toBlack()
    {
        var oDiv=document.getElementById(‘div1‘);
        oDiv.style.background=‘black‘;
        }*/        
</script>
</head>

<body>
    <input type="button" value="變綠" onclick="setColor(‘green‘)"/>
    <input type="button" value="變黃" onclick="setColor(‘yellow‘)"/>
    <input type="button" value="變黑" onclick="setColor(‘black‘)"/>
    <div id="div1">
    </div>
</body>
</html>

比如改變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=utf-8" />
<title>無標題文檔</title>
<style>
#div1
{
    width:200px;
    height:200px;
    background:red;
    }
</style>
<script>
function setStyle(name,value)
{
    var oDiv=document.getElementById(‘div1‘);
    oDiv.style[name]=value;
    }
</script>
</head>

<body>
<input type="button" value="變寬" onclick="setStyle(‘width‘,‘400px‘)"/>
<input type="button" value="變高" onclick="setStyle(‘height‘,‘400px‘)"/>
<input type="button" value="變綠" onclick="setStyle(‘background‘,‘green‘)"/>
<div id="div1">
</div>
</body>
</html>

  字串和變數——區別和關係

  變數裡面的值是可以改變的。

  比如var a=5;alert(a);那麼瀏覽器彈出來的就是5。如果a=12,那麼瀏覽器彈出來的就是12。

  而字串的值是固定的。比如alert(‘abc‘);那麼瀏覽器彈出來的就是abc。

  CSS裡面還有一種東西叫做字面量(常量),就是說你看到這個東西,自然就知道這個代表什麼。比如12,就是數字12;比如‘abc‘,就是字母abc。

  與字面量相對的就是變數。比如var a,a中的值是不固定的,僅僅只看a根本不知道a代表什麼值。

  因此寫字串時必須加‘‘單引號,如果不加就會被認為是變數或者參數。

 

【JS學習筆記】函數傳參

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.