[英]Trigger inline onchange javascript
I'm writing a greasemonkey script that creates an auto-complete search type box which makes it easier to select from a large dropdown on a webpage. 我正在编写一个acobsmonkey脚本,该脚本创建一个自动完成的搜索类型框,使从网页上的较大下拉列表中进行选择更加容易。 The dropdown has inline onchange code which I can't seem to get to trigger when I change the selection using javascript. 下拉列表具有内联onchange代码,当我使用javascript更改选择时似乎无法触发。 Any ideas? 有任何想法吗?
Suppose the the page had something like: 假设页面具有以下内容:
<input onchange="someFunction()">
Then your Greasemonkey JavaScript could change the input value and then call the function using: 然后,您的Greasemonkey JavaScript可以更改输入值,然后使用以下命令调用该函数:
unsafeWindow.someFunction();
You can trigger the onchange "really": 您可以“真正”触发onchange:
From within GM: 从通用汽车内部:
unsafeWindow.dropdownObject.onchange();
From the webpage: 在网页上:
dropdownObject.onchange();
For tampermonkey, this works simply without using unsafewindow. 对于tampermonkey,这很简单,无需使用unsafewindow。
Suppose the dropdownbox is defined in webpage something like: 假设下拉框是在网页中定义的,例如:
<select id="dd_id", onchange="myfunction()">
Then following code in userscript triggers the onchange event: 然后,用户脚本中的以下代码触发onchange事件:
document.getElementById('dd_id').onchange();
Also, it is suggested to avoid use of unsafewindow in documentation. 另外,建议避免在文档中使用unsafewindow。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.