Assign div text to variable then show it

后端 未结 3 1126
一整个雨季
一整个雨季 2021-01-27 08:39

I have a simple task I\'m trying to accomplish learning JavaScript but haven\'t been able to find a clear answer. Here\'s the code;



        
3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-27 09:04

    What you're doing is storing the DOM Object in the variable, not the text. You should access the innerHTML property to access the text.

    var t = document.getElementById("foo").innerHTML;
    t = t.trim(); // to remove the whitespaces before and after the div.
    document.write(t); 
    

提交回复
热议问题