DOM删除元素和更改css样式等方法

    1.创建元素
    首先创建该元素(元素节点),然后向一个已存在的元素追加该元素。
    var p3=document.createElement("p");
    var node=document.createTextNode("you are beautiful!");//给元素内添加内容
    p3.appendChild(node);
    var par=document.getElementById("div1");
    par.appendChild(p3);
    2.删除元素
    var p1=document.getElementById("p1");//首先获取父元素,
    par.removeChild(p1);//在父元素下删除
    3.改变元素中的内容
    var p2=document.getElementById("p2").innerHTML="sdsfawsv";
    将another paragraph.改为sdsfawsv
    4.更改元素css样式
    var par=document.getElementById("div1").style.background ="red";

猜你喜欢

转载自blog.csdn.net/yezi__6/article/details/81879635
今日推荐