을 클릭하면
function fff() {
var ss = document.createElement('span');
var text = document.createTextNode('https://www.google.com/');
ss.appendChild(text);
document.getElementById('water').appendChild(ss);
}
function ff() {
var s = document.createElement('a');
var t = document.createTextNode("https://www.google.com/");
s.appendChild(t);
s.href = "http://example.com";
var item = document.getElementById('water');
item.replaceChild(s, item);
}
<body onload="fff()">
<span id="water"></span> <br><br>
<button onclick="ff()">Replace with anchor</button>
</body>
은 내가이 얻을. span # water를 바꾸려면 부모 (즉, <body>
)를 선택하고 replaceChild를 호출해야합니다. 당신은 ff의 마지막 라인을 다음과 같이 바꿀 수도 있습니다
var body = document.body;
body.replaceChild(s, item);
또는보다 일반적인 해결책은's.parentNode.replaceChild (...)'입니다. – CBroe