day03

A.今天你學(xué)到了什么

1.添加子元素節(jié)點(diǎn)方法

    <div>
        <input type="text" id="txt"> <button id="add">添加</button>
    </div>
    <ul id="parent">

    </ul>
      <script>
        var txt =document.getElementById("txt");
        var add=document.getElementById("add");
        var parent=document.getElementById("parent");
        add.onclick=function(){
            var value=txt.value;
            var li=document.createElement("li");
            li.innerHTML=value;
            console.log(li);
            parent.appendChild(li);
       }
    </script>

2.獲取父元素節(jié)點(diǎn)

 <div class="parent">
        <p id="child">hello world</p>
    </div>
    
    <script>
        //parentNode 獲得父節(jié)點(diǎn)
         var  p=document.getElementById("child");
         var parent=p.parentNode;
         console.log(parent);
    </script>

3.界面刪除父節(jié)點(diǎn)

<ul>
        <li>小米手機(jī)<a href="#">刪除</a></li>
        <li>蘋果手機(jī)<a href="#">刪除</a></li>
        <li>華為手機(jī)<a href="#">刪除</a></li>
    </ul>
    <script>
        var shows=document.getElementsByTagName("a");
        for(let i=0;i<shows.length;i++){
            shows[i].onclick=function(){
                this.parentNode.style.display="none";
                
            }
        }
    </script>

4.更改子節(jié)點(diǎn)屬性

<ul id="parent">
        <li>1</li>
        <li>2</li> 
        <li>3</li>
        <li>4</li>
    </ul>
    <script>
        let parent = document.getElementById("parent");
        let childs=parent.childNodes;
        // console.log(childs); 獲得所有類型的子節(jié)點(diǎn)
        for(let i=0;i<childs.length;i++){
            if(childs[i].nodeType==1){
                childs[i].style.backgroundColor="red";
            }
        }
    </script>

5.獲取子節(jié)點(diǎn)

<ul id="parent">
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
    <script>
    // firstChild 獲取第一個(gè)子節(jié)點(diǎn)
    // lastChild 獲取最后一個(gè)子節(jié)點(diǎn)
    // firstElementChild 獲取第一個(gè)元素子節(jié)點(diǎn)  i9以下不支持
    // lastElementChild  獲取最后一個(gè)元素子節(jié)點(diǎn)
        var parent=document.getElementById("parent");
        var fc=document.getElementById("parent").firstChild;
        var fElement=parent.firstElementChild;
        
        console.log(fc);
        console.log(fElement);
    </script>

6.兄弟節(jié)點(diǎn)

 <!-- previousSibling 獲取前面的兄弟節(jié)點(diǎn)
        previousElementSibling 獲取獲取前面的兄弟元素節(jié)點(diǎn)
        nextSibings 獲取下一個(gè)兄弟節(jié)點(diǎn)
        nextElementSibings 獲取下一個(gè)兄弟元素節(jié)點(diǎn) -->
    <div>
        <p>hello world 01</p>
        <p id="test">hello world 02</p>
        <p>hello world 03</p>
    </div>
    <script>
        let test =document.getElementById("test");
        let pSiblings=test.previousSibling;
        let eSiblings=test.previousElementSibling;
        console.log(pSiblings);
        console.log(eSiblings);
    </script>

7.獲取元素偏移量

 <div id="test">
    //需要設(shè)置div元素的大小苛让,margin值
    </div>
    <script>
        // offsetLeft   獲取元素水平方向偏移位置
        // offsetTop     獲取元素水平方向偏移位置
        // offsetWidth     獲取元素的寬度, 特點(diǎn):只讀屬性
        // offsetHeight    獲取元素的高度
        let test=document.getElementById("test");
        let offl=test.offsetLeft;
        let offt=test.offsetTop;
        let offw=test.offsetWidth;
        let offh=test.offsetHeight;
        console.log(offl);
        console.log(offt);
        console.log(offw);
        console.log(offh);
    </script>

8.定時(shí)器

<script>
        //setTimeout 超時(shí)調(diào)用
        //語(yǔ)法:setTimeout 間隔一段時(shí)間脖旱,觸發(fā)函數(shù)纲缓,只觸發(fā)一次
       setTimeout(function(){
           alert(1)
       }, 2000);
        //使用setTimeout實(shí)現(xiàn)setInterval       持續(xù)觸發(fā),間隔時(shí)間為1000毫秒
        // function  go(){
        //     alert("hello world")
        //     setTimeout(go,1000)
        // }  
        // go();       
 </script>

9.設(shè)置屬性的方式

9.1第一種
<p id="test">hello world</p>
    <script>
       var p =document.getElementById("test");
        // test.style.color="red";
        // test.style["color"]="red";  什么時(shí)候使用:當(dāng)傳入的屬性是變量的時(shí)候
         function changeStyle(ele,attr,value){
             ele.style[attr]=value;
         }
        //  changeStyle(p,"background","green");
    </script>
9.2第二種
 <!-- 設(shè)置屬性的方式,設(shè)置樣式 -->
    <p id="test">hello world</p>
    <script>
        let test =document.getElementById("test");
        test.setAttribute("Style","color:red");
    </script>

10 操作屬性

 <p class="one" id="test">
        hello world
   </p> 
   <script>
    //    getAttribute()     得到屬性值
    //    setAttribute()     設(shè)置屬性值
    //    removeAttrbute()   移除屬性
       let test =document.getElementById("test");
       
        test.setAttribute("class","two")
        let cValue=test.getAttribute("class");
       console.log(cValue);
   </script>

11.動(dòng)態(tài)添加 title

<p id="test">hello world</p>
    <script>
        let test=document.getElementById("test");
        test.setAttribute("title","title");
    </script>

12.獲取瀏覽器窗口大小

<script>
        var windowWidth=window.innerWidth;
        var windowHeight=window.innerHeight;
        console.log(windowWidth); //寬度
        console.log(windowHeight);//高度
    </script>

13.獲取頁(yè)面內(nèi)容窗口大小

<div>      //  需要設(shè)置div屬性</div>
    <script>
        var width=document.documentElement.scrollWidth;
        var height=document.documentElement.scrollHeight;
        console.log(width+":"+height);
 </script>

14.frame 文檔碎片

<ul id="parent"></ul>
    <button id="btn">btn</button>
    <script>
/*
        createDocumentFragment()文檔碎片 好處:提高渲染效率 
         */
        var btn=document.getElementById("btn");
        var parent=document.getElementById("parent");
        var frame=document.createDocumentFragment();
        btn.onclick=function(){
            for(let i=0;i<10;i++){
                let i=document.createElement("li");
                frame.appendChild(i);
            }
            parent.appendChild(frame);
           
        }
    </script>

15. 箭頭函數(shù)

 <script>
        var one=function(){
            console.log("one");
        }
      var  go=()=>{
            console.log("hello world");
     }
        go();
    </script>

16.函數(shù)的傳參

 // JS中函數(shù)傳不定參
        function go(a,b){
            console.log(a);
            console.log(a+b);
            console.log("沒有參數(shù)");
        }
        go();
        //js中  如何實(shí)現(xiàn)重載  使用arguments對(duì)象
        function test(){
            console.log(arguments);
        }
        test(2,3,5)

17.js 中實(shí)現(xiàn)函數(shù)的重載

    <script>
        function test(){
            if(arguments.length==1){
                cosole.log(argument[0])
            }if(arguments.length==2){
                console.log(argument[0]+argument[1]);                   fdsf
            }else{
                console.log("hello world")
            }
        }
        test();
        test(1);
        test(1,3);
    </script>

18.基本數(shù)據(jù)類型和引用

<script>
        // 1.基本類型只傳值
        // 2.引用類型傳值又傳址
         //  var a =10;
        //  var b = a;
        //  console.log(b);
        var arr = [1,2,3];
        var b = arr;
        arr[arr.length] =4;
        var obj = {
            name:"chengchao"
        }
        var jiang = obj;
        obj.age = 30;
        console.log(jiang);
  </script>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末蔼两,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子逞度,更是在濱河造成了極大的恐慌额划,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,509評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件档泽,死亡現(xiàn)場(chǎng)離奇詭異俊戳,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)馆匿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門抑胎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人渐北,你說(shuō)我怎么就攤上這事阿逃。” “怎么了赃蛛?”我有些...
    開封第一講書人閱讀 163,875評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵恃锉,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我呕臂,道長(zhǎng)破托,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,441評(píng)論 1 293
  • 正文 為了忘掉前任歧蒋,我火速辦了婚禮土砂,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘疏尿。我一直安慰自己瘟芝,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評(píng)論 6 392
  • 文/花漫 我一把揭開白布褥琐。 她就那樣靜靜地躺著锌俱,像睡著了一般。 火紅的嫁衣襯著肌膚如雪敌呈。 梳的紋絲不亂的頭發(fā)上贸宏,一...
    開封第一講書人閱讀 51,365評(píng)論 1 302
  • 那天造寝,我揣著相機(jī)與錄音,去河邊找鬼吭练。 笑死诫龙,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的鲫咽。 我是一名探鬼主播签赃,決...
    沈念sama閱讀 40,190評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼分尸!你這毒婦竟也來(lái)了锦聊?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,062評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤箩绍,失蹤者是張志新(化名)和其女友劉穎孔庭,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體材蛛,經(jīng)...
    沈念sama閱讀 45,500評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡圆到,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評(píng)論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了卑吭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片芽淡。...
    茶點(diǎn)故事閱讀 39,834評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖陨簇,靈堂內(nèi)的尸體忽然破棺而出吐绵,到底是詐尸還是另有隱情,我是刑警寧澤河绽,帶...
    沈念sama閱讀 35,559評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站唉窃,受9級(jí)特大地震影響耙饰,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜纹份,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評(píng)論 3 328
  • 文/蒙蒙 一苟跪、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧蔓涧,春花似錦件已、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至茉盏,卻和暖如春鉴未,著一層夾襖步出監(jiān)牢的瞬間枢冤,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工铜秆, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留淹真,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,958評(píng)論 2 370
  • 正文 我出身青樓连茧,卻偏偏與公主長(zhǎng)得像核蘸,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子啸驯,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案值纱? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 13,751評(píng)論 1 92
  • A.今天學(xué)什么? 1.定時(shí)器timer 1.1 setTimeout --> 超時(shí)調(diào)用語(yǔ)法:setTimeout(...
    克馬閱讀 199評(píng)論 0 0
  • 第3章 基本概念 3.1 語(yǔ)法 3.2 關(guān)鍵字和保留字 3.3 變量 3.4 數(shù)據(jù)類型 5種簡(jiǎn)單數(shù)據(jù)類型:Unde...
    RickCole閱讀 5,126評(píng)論 0 21
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理坯汤,服務(wù)發(fā)現(xiàn)虐唠,斷路器,智...
    卡卡羅2017閱讀 134,656評(píng)論 18 139
  • 文|孟永輝 新零售時(shí)代的來(lái)臨讓越來(lái)越多的行業(yè)開始面臨一場(chǎng)全新變革,改變?cè)械臓I(yíng)銷方式搓幌、運(yùn)營(yíng)方式以及商品生產(chǎn)方式正在...
    孟永輝閱讀 215評(píng)論 0 0