我想替换下面按钮的一个变量:
<script type="text/javascript">
var btn = window.document.getElementById('btn_fb_1');
</script>
<button id="btn_fb_1" type="button">Log into Somewhere<br />(Joe Doe)</button>
使用上面的“btn”变量,如何用这样的文本替换整个元素(注意我需要保留“Joe Doe”:
Connected to Somewhere<br />(Joe Doe)
最佳答案
试试这个代码:
var btn = window.document.getElementById('btn_fb_1');
var d = document.createElement('div');
var name = btn.innerHTML.match(/\(.*\)/);
d.innerHTML = 'Connected to Somewhere<br />'+name;
btn.parentNode.replaceChild(d, btn); // replace btn with d
关于javascript - 帮助通过 Javascript 将一个元素解析为另一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3679772/