JavaScrip--學習3

DOM:

//location對象 可以定位到指定地方 history可以back go

function locationFuc() {
    var body = document.getElementsByTagName("body")[0];
    for (var prop in location) {
        var elem = document.createElement("p");  //創建一個p節點
        var text = document.createTextNode(prop+"::"+location[prop]);  //創建一個文本節點
        elem.appendChild(text);
        body.appendChild(elem);
    }

var pNodes = document.getElementsByTagName("p");  //根據標籤的 名稱獲取對應的標籤組
    for (var i = 0;i <pNodes.length;i++) {
        pNodes[i].style.background = "#ff0000";
    }
    return true;
}


function changeTxt() {
    var pNode = document.getElementById("content");  //獲取到id爲content的節點
    alert(pNode.innerHTML);
    //修改獲得p節點中的文本內容
    pNode.innerHTML = "there is a error";

    var attrList = "";
    for (var attr in pNode) {  //遍歷出來的是節點的屬性名
        attrList += pNode.getAttribute(attr);  //根據屬性名稱獲取屬性值
    }
    pNode.innerHTML = attrList;
    //設置屬性的值
    pNode.setAttribute("href", "http://www.baidu.com");
    alert(pNode.getAttribute("href"));
}

//創建節點

function createNodes() {
    //創建p  a  節點
    var pNode = document.createElement("p");
    var aNode = document.createElement("a");
    //創建文本節點
    var txt = document.createTextNode("I created nodes");

//給創建的a節點 設置屬性
aNode.setAttribute("href","http://www.baidu.com");
aNode.setAttribute("background","#00aa00");
pNode.setAttribute("id","p_node");

//將創建的節點添加到 body裏面去
var body = document.getElementsByTagName("body")[0];
aNode.appendChild(txt);
pNode.appendChild(aNode);
body.appendChild(pNode);
//刪除節點
var a = document.getElementById("p_node");
document.body.removeChild(a);  //只能刪除直接子節點

}

//定時任務

function timer() {
    alert("你點啊,繼續點");
    var tag = setInterval("timer()", 1000);  //定時任務  1秒後執行 timer這個任務  返回一個tag 結合
    //clearInterval(tag);  該方法清除 該定時任務
}

function timeOut() {
    alert("click,go on");
    var tag = setTimeout("timeOut()", 1000);  //定時任務
}

博客轉移到個人站點:http://www.wangchengmeng.club/2018/02/01/JavaScrip–%E5%AD%A6%E4%B9%A03/

歡迎來吐槽

發佈了49 篇原創文章 · 獲贊 38 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章