How to get innerHTML from a div?

时光总嘲笑我的痴心妄想 提交于 2020-01-03 20:03:11

问题


I need to save the innerHTML of a div and store it in a cookie. Here is my basic code.

//The div-saving function
function addStatus(sName){
    getElement("bottomDiv").innerHTML += sName + "<br>";
    document.cookie="status=" + sName + "<br>";
}

//The button clicking function
function clk(){
    num++;
    document.cookie = "num=" + num;
    switch(num){
        case 25:
            addStatus("25: Alright! Let the games begin!");
        break;
        case 50:
            addStatus("50: Woah! Getting into the high numbers!");
        break;
    }
}

What ends up happening is only the last achieved status gets saved. I need to know how to get the innerHTML of the div instead. Thank you.


回答1:


Try this..

 var div= document.getElementById("bottomDiv").innerHTML;



回答2:


var holder = document.getElementById("bottomDiv").innerHTML;

The above will get the innerHTML value and assign it to the variable. Also there is no built in function getElement , you can use getElementById, there are other variations as well.



来源:https://stackoverflow.com/questions/23190848/how-to-get-innerhtml-from-a-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!