gpt4 book ai didi

javascript - 添加一个单击时调用内容脚本函数的按钮?

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

我在 Google Chrome 扩展的 content.js 中有以下代码:

jQuery(document).ready(function () {
jQuery("body").html('<input type="button" id="soso" value="asd" onclick="goFrame()" />');
});
function goFrame() {
alert('Value');
}

按钮已成功创建,但当我点击它时,消息没有出现,我在 Google Chrome 控制台中收到以下错误:

Uncaught ReferenceError: goFrame is not defined

最佳答案

先读 Chrome extension code vs Content scripts vs Injected scripts .

要解决这个问题,摆脱内联事件监听器,动态绑定(bind)事件:

    ...
var $input = $('<input type="button" id="soso" value="asd">').click(goFrame);
jQuery("body").html($input);
});
function goFrame() {
alert('Value');
}

关于javascript - 添加一个单击时调用内容脚本函数的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11703492/

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