JS學習筆記--仿手機發送內容互動

來源:互聯網
上載者:User

標籤:style   blog   java   color   os   strong   

學習JS筆記----記錄上課中學習的知識點,分享下老師教的內容:

 

1.html內容

<div id="box">    <div id="message">        <!--<p class="left">            <img src="img/136.jpg" />            <span>這是第一句話</span>        </p>        <p class="right">            <img src="img/137.jpg" />            <span>這第二句這是第二句這是第二句話</span>        </p>-->    </div>    <img id="small-pic" src="img/150.jpg" />    <input id="text1" type="text" />    <input id="btn1" type="button" value="提交" />    <strong id="txt">0</strong>    <a class="active" href="javascript:;" id="up">上</a>    <a href="javascript:;" id="down">下</a></div>

 

2.JS效果

<script>var oDiv = document.getElementById(‘message‘);var oImg = document.getElementById(‘small-pic‘);var oText = document.getElementById(‘text1‘);var oBtn = document.getElementById(‘btn1‘);var aUp = document.getElementById("up");var aDown = document.getElementById("down");var oTxt = document.getElementById("txt");var onOff = true;var dir = true;var num = 0;oDiv.onclick =function () {    oDiv.innerHTML = ‘‘;    oTxt.innerHTML = 0;    num = 0;}oImg.onclick = function () {    if ( onOff ) {        oImg.src = ‘img/151.jpg‘;        onOff = false;    }    else {        oImg.src = ‘img/150.jpg‘;        onOff = true;    }};aUp.onclick = function () {    aUp.className = ‘active‘;     aDown.className = ‘‘;    dir = true;}aDown.onclick = function () {    aUp.className = ‘‘;     aDown.className = ‘active‘;      dir = false;   }oBtn.onclick = function () {    if ( oText.value == ‘‘ ) { //input值為空白彈框        alert(‘請輸入內容‘);    }    else {                // alert( onOff );    // true => ‘left‘        // alert( onOff );    // false => ‘right‘        var sClass = ‘‘;        if ( onOff ) {            sClass = ‘left‘;        }        else {            sClass = ‘right‘;        }                if (dir) {  //新增內容在上             oDiv.innerHTML = ‘<p class="‘ + sClass + ‘">‘ +                    ‘<img src="‘ + oImg.src + ‘" />‘ +                    ‘<span>‘ + oText.value + ‘</span>‘ +                ‘</p>‘ + oDiv.innerHTML;           }else{    //新增內容在下            oDiv.innerHTML += ‘<p class="‘ + sClass + ‘">‘ +                    ‘<img src="‘ + oImg.src + ‘" />‘ +                    ‘<span>‘ + oText.value + ‘</span>‘ +                ‘</p>‘ ;            }                oText.value = ‘‘; //提交時input值為空白                num++  //數字隨著提交+1        oTxt.innerHTML = num;     } };</script>

 

3.展示

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.