我正在尝试创建一个 onclick 元素并在单击该元素时隐藏该元素,但它什么也不做?
为什么 hide() 函数不执行任何操作?
<script type="text/javascript">
function show(){
var a = document.getElementById('foo');
var b = document.createElement("div");
b.setAttribute('id', 'bar');
b.setAttribute('onclick', 'hide()');
a.appendChild(b);
b.innerHTML = 'TEXT CONTENT';
b.onclick = function() {
hide();
};
}
function hide() {
var x = document.getElementById('foo');
var z = document.getElementById('bar');
x.removeChild(z);
}
</script>
<div id="foo" onclick="show()">CLICK ME</div>
最佳答案
添加 b.onclick = function() {hide();};
关于JavaScript 做了一些奇怪的事情?可以在 IE 下运行,但不能在 Firefox 下运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5335470/