我在使用这两个事件时遇到了一些问题,它们工作得很好,但是我遇到了一个问题:我在div中有一个div。
function addButtons() {
var node = document.createElement('div');
var obj = document.getElementById('container');
node.setAttribute('id', 'buttons');
node.innerHTML = '<input type="button" value="B" onClick="document.execCommand(\'bold\',false)" />';
obj.appendChild(node);
}
function removeButtons(id) {
var obj = document.getElementById('container');
obj.removeChild(obj.childNodes[3]);
}http://jsfiddle.net/bnjGE/2/
如果我删除removeButtons()函数,它可以正常工作,但会创建多个按钮。
谢谢你
发布于 2012-12-29 11:26:21
您可以在按钮f.ex上的mousedown事件上添加return false;语句:
node.getElementsByTagName('input')[0].onmousedown = function(e) {
return false;
};http://jsfiddle.net/bnjGE/8/
发布于 2012-12-29 11:28:15
document.onclick = function(e){
console.log(e.target);
if(e.target.id != 'buttons' && e.target.parentNode.id != 'buttons' && e.target.id != 'textarea'){
removeButtons();
}
}http://jsfiddle.net/bnjGE/7/也许这是一个解决方案,但并不完美。
https://stackoverflow.com/questions/14078337
复制相似问题