gpt4 book ai didi

javascript - 使用 appendChild() 添加 oncontextmenu

转载 作者:行者123 更新时间:2023-11-30 18:14:04 27 4
gpt4 key购买 nike

嘿,如果您使用 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);

http://jsfiddle.net/mowglisanu/ZNvhD/

关于javascript - 使用 appendChild() 添加 oncontextmenu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13795564/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com