Javascript 在Electron中将eventListener或attachEvent添加到锚定标记的正确方法是什么?

Javascript 在Electron中将eventListener或attachEvent添加到锚定标记的正确方法是什么?,javascript,html,node.js,electron,Javascript,Html,Node.js,Electron,我想为我的Electron应用程序的index.html中的锚标记添加NodeJS模块函数的eventListener。我一直在谷歌上搜索解决方案,但似乎都不管用。我得到了一个概念,这与电子有关。如果这与主进程和渲染器进程有关,请让我知道。到目前为止,我还没有使用ipc,因为它是不需要的 我的script.js文件如下所示: const nodejsModule = require('module'); var anchorTag = document.getElementById("a1")

我想为我的Electron应用程序的index.html中的锚标记添加NodeJS模块函数的eventListener。我一直在谷歌上搜索解决方案,但似乎都不管用。我得到了一个概念,这与电子有关。如果这与主进程和渲染器进程有关,请让我知道。到目前为止,我还没有使用ipc,因为它是不需要的

我的script.js文件如下所示:

const nodejsModule = require('module');

var anchorTag = document.getElementById("a1");
if (typeof window.addEventListener != "undefined") {
    anchorTag.addEventListener("click",nodejsModule.function()),false);
} else {
    anchorTag.attachEvent("onclick",nodejsModule.function());
}
我也尝试过:

const nodejsModule  = require('module');
const anchorTag = document.getElementById('a1');
anchorTag .addEventListener('click', function(e) {
    nodejsModule.function();
});
在my index.html中,我有:

<a  href="#" id="a1" >Click me!</a>
<script src="script.js" type="text/javascript"></script>

我还尝试将脚本放在锚标记中。我没有在main.js中处理这段代码,因为我只想将函数链接到我的锚标记。可能是因为我使用了require('module'),这是NodeJS语法,这在我的HTML代码中不起作用?这是我的第一个猜测,因为当我在没有事件侦听器的情况下运行script.js时,只包含require和函数,函数的工作确实很有魅力。另一个模块。我曾经

<script src="../node_modules\exampleModule\module.js" type="text/javascript"></script>

直接在my index.html中。因此,我删除了script.js中的require,它成功了。我在当前模块中尝试过这个,但没有成功。我认为这是因为在模块中涉及到一些Python和C++(它有VisualStudio和Python作为一个要求)。
您能帮助我了解在电子环境中实现此功能的正确方法吗?

请确保在要添加侦听器的标记之后启动脚本标记

<body>
    /* all of your code here */
    <a  href="#" id="a1" >Click me!</a>
    <script src="script.js"></script>
</body>

在创建浏览器窗口时,还可以尝试使节点集成
true

    const mainWindow = new BrowserWindow({
        width: 1100,
        height: 700,
        chromeWebSecurity: false,
        webPreferences: {
            nodeIntegration: true,
            webSecurity: false
        }
    })

确保脚本标记在要向其添加侦听器的标记之后启动

<body>
    /* all of your code here */
    <a  href="#" id="a1" >Click me!</a>
    <script src="script.js"></script>
</body>

在创建浏览器窗口时,还可以尝试使节点集成
true

    const mainWindow = new BrowserWindow({
        width: 1100,
        height: 700,
        chromeWebSecurity: false,
        webPreferences: {
            nodeIntegration: true,
            webSecurity: false
        }
    })