I'm using google iconfonts in my website.
Normally,the code in HTML is like this:
<i class="material-icons"></i>
and it will display a 'time' icon on the page: the icon will display
But now I want to add a icon to DOM by using appendChild() method.the code is:
var my_element = document.getElementById("icon-div");
// create a i tag
var i = document.createElement("i");
i.className = "material-icons";
i.textContent = "";
// append the tag to my_element
my_element.appendChild(i);
<html>
<head>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<body>
<!--it should be-->
<i class="material-icons"></i>
<!--add it by using appendChild method-->
<div id="icon-div">
</div>
</body>
</html>
When I check the <i>
(the one in div) in broswer console,its textContent is ""
,and its innerHTML is "&#xe192;"
;
So how can I get a icon when I'm add a icon to DOM dynamically.