简体   繁体   English

如何在js中修改oncontextmenu

[英]how modify oncontextmenu in js

I'm trying to change an existing "oncontextmenu" of a div. 我正在尝试更改div的现有“oncontextmenu”。

The div: div:

<td id="linetitle_title1" oncontextmenu="LoadLineXml('dGl0bGUx')">

The js: js:

var linekey = window.btoa(document.getElementById('updateTitle').value);
var linefunction = "LoadLineXml(\'"+linekey+"\')";
document.getElementById('linetitle_' + input ).oncontextmenu = linefunction;

Any idea for change it ? 有改变的想法吗?

The oncontextmenu property accepts a function, but you'll probably also need to wrap that function in an IIFE to protect the scope of linekey depending on how that function is called: oncontextmenu 属性接受的函数,但是你可能还需要换行功能的IIFE保护的范围linekey取决于函数是如何被调用:

document.getElementById('linetitle_' + input ).oncontextmenu = (function(key) {
    return function() {
        LoadLineXml(linekey)
    }
}(linekey));

Which is what you should be doing instead of setting attributes within HTML. 这是你应该做的,而不是在HTML中设置属性。 You could set the attribute directly to override the attribute which is already there: 您可以直接设置属性以覆盖已存在的属性:

document.getElementById('linetitle_' + input ).setAttribute("oncontextmenu", linefunction);

But I wouldn't recommend that approach. 但我不建议采用这种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM