Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Greasemonkey-按钮添加到<;部门>;我补充道,没有调用我的greasemonkey函数_Javascript_Html_Ajax_Button_Greasemonkey - Fatal编程技术网

Javascript Greasemonkey-按钮添加到<;部门>;我补充道,没有调用我的greasemonkey函数

Javascript Greasemonkey-按钮添加到<;部门>;我补充道,没有调用我的greasemonkey函数,javascript,html,ajax,button,greasemonkey,Javascript,Html,Ajax,Button,Greasemonkey,我通过GreaseMonkey添加了一个按钮: var input = document.createElement('input'); input.type = 'button'; input.value = 'Off'; input.onclick = turnTheWheelsofTime; 当我使用以下方法将此按钮添加到页面时,此按钮工作正常: document.body.appendChild(input); 然而,我想将我的按钮放置在页面上一个不移动的框中,而不是仅仅将其附加到末

我通过GreaseMonkey添加了一个按钮:

var input = document.createElement('input');
input.type = 'button';
input.value = 'Off';
input.onclick = turnTheWheelsofTime;
当我使用以下方法将此按钮添加到页面时,此按钮工作正常:

document.body.appendChild(input);
然而,我想将我的按钮放置在页面上一个不移动的框中,而不是仅仅将其附加到末尾(这是我真正关心的问题,下面是我如何应用它的,因此如果您知道更好的方法,我很乐意听到)

我找到了一个使用AJAX的解决方案。我在脚本中添加了以下内容:

// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
 // @grant       GM_addStyle`<br><br>
$("body").append ( '\
    <div id="displayBox">\
    <p id="displayBoxText">Test</p>\
    <button id="gmCloseDlgBtn" type="button">Close popup</button>\
    </div>\
' );
而不是前面的:

document.body.appendChild(input);
这工作得有点完美。该框已添加到我的
,我没有在问题中粘贴此框,但我使用了一些带有GM_addStyle的CSS,所有这些都已应用到我的
。一切看起来都很漂亮,但按钮什么也没用


为什么??我可以让它做一个
console.log('test'),工作正常,但当我尝试使用Greasemonkey脚本中的函数时,什么也没发生?

如果有人好奇,我就发现了错误。这实际上与在我的脚本中引入@grant有关。 我的函数turnTheWheelsofTime使用了页面中的javascript函数,并且显然使用@grant沙盒处理一切。
@grant中的沙盒实际上破坏了函数

我觉得这与问题无关,但这就是gmCloseDlgBtn的工作方式:
/---使用jQuery激活对话框按钮$(“#gmCloseDlgBtn”)。单击(函数(){$(“#显示框”).hide();})此按钮工作正常,但当我尝试使用此方法添加按钮时,它也失败了。我认为这是因为我的Greasemonkey函数超出了html中页面的范围,这就是为什么我很困惑为什么它没有按照我在问题中所问的方式工作。fyi,这不是“使用AJAX”,而是使用jQuery。
document.body.appendChild(input);