嘿,如果您使用 appendChild,是否可以通过 JavaScript 将 oncontextmenu 添加到元素???
我这样设置我的div:
var d = document.createElement('div');
d.id = 'G';
d.className = 'Ouput';
document.getElementById('E').appendChild(d);
但我想知道如何添加:
oncontextmenu="return false;"
它可以在我追加子项之前添加到 Div 属性中,还是我必须在之后添加它?
最佳答案
您可以在将 div 添加到 dom 之前或之后添加 oncontextmenu,但使用函数而不是字符串。
var d = document.createElement('div');
d.id = 'G';
d.className = 'Ouput';
d.oncontextmenu = function(){return false};
document.getElementById('E').appendChild(d);
关于javascript - 使用 appendChild() 添加 oncontextmenu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13795564/