每当特定的<选项>出现时,就从 Greasemonkey 脚本更改页面。被选中选项>
在使用 jQuery 和“tamtam”编写 GreaseMonkey 脚本时,我的脑海中出现了一个问题。
有一个页面(我无法编辑)有一个带有四个选项的
<select id="idname" name="namename">
<option>Option1</option>
<option selected="selected">Option2</option>
<option>Option3</option>
<option>Option4</option>
</select>
现在我想在选择 Option1 时调用脚本。 Option1 通向我想要插入脚本的另一个站点。
我该如何写这样的内容:
if (Option1 is selected) {
perform the script
}
像下面这样的东西会起作用吗?
if(document.getElementById("idname").getElementsByTagName("option")[0].onselect == true){
perform the script
}
如果没有,您可以发布一个参考资料来帮助我吗?
编辑
我能够创建一个函数和一个事件处理程序。
function myCompute(Event) {
with (this) {
var myTest = value;
if (value == "option1") {
$("#tabelle").show();
}
else {
$("#tabelle").hide();
}
}
}
事件处理程序:
$("#option-type").change (myCompute);
$("#tabelle").hide();
它的工作原理如下: 通过选择选项 2,3 或 4,表格将被隐藏。通过选择选项 1,将显示表格。 通过访问该站点,大多数情况下都会选择选项 2,并且不会显示任何内容。 现在我得到的情况是,通过访问该站点选择了选项 1,并且也没有出现表格。我的想法是,当预选 option1 时应该显示该表。我认为缺少 EventHandler。 就像你一样,布洛克·亚当斯说。
$("#option-type option:first").select (myCompute);
$("#option-type").change (myCompute);
$("#tabelle").hide();
如果我将函数与 $("#tabelle").hide();
绑定,则该表从一开始就被隐藏。通过将选项更改为 option1,将显示该表。选择选项 1 时如何显示表格以及选择选项 2、3、4 时如何隐藏表格?
尝试 option:first
会导致“未知伪元素”错误。
While writing a GreaseMonkey script using jQuery and "tamtam", a question arose in my mind.
There is a page (which I can't edit) having a <select>
element with four options. Option 2 is preselected.
<select id="idname" name="namename">
<option>Option1</option>
<option selected="selected">Option2</option>
<option>Option3</option>
<option>Option4</option>
</select>
Now I'd like to call the script when Option1 is selected. Option1 leads to another site I want to insert my script.
How would I write something like:
if (Option1 is selected) {
perform the script
}
Would something like the following work?
if(document.getElementById("idname").getElementsByTagName("option")[0].onselect == true){
perform the script
}
If not, can you post a reference helping me?
Edit
I was able to create a function and an event handler.
function myCompute(Event) {
with (this) {
var myTest = value;
if (value == "option1") {
$("#tabelle").show();
}
else {
$("#tabelle").hide();
}
}
}
Event handlers:
$("#option-type").change (myCompute);
$("#tabelle").hide();
It works as follows:
By choosing option 2,3 or 4 the table is hidden. By choosing option 1 the table is shown.
By visiting the site option 2 is selected most of the time and nothing is shown.
Now I got the case that option 1 is selected by visiting the site and no table appears, too. My idea was that the table should be shown when option1 is preselected.. I think that an EventHandler is missing.
Like you, Brock Adams, said.
$("#option-type option:first").select (myCompute);
$("#option-type").change (myCompute);
$("#tabelle").hide();
If I bind the function with $("#tabelle").hide();
, the table is hidden from the very beginning. By changing the options to option1 the table is shown. How can I show the table when option 1 is selected and how can I hide the table when option 2,3,4 are selected?
Trying option:first
results in an "unknown pseudo-element" error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新:
好的,如果我理解修改后的问题,那么代码现在可以按预期工作,除非选项 1 按所选方式启动。 (PS,应该编辑给定代码的 id 以匹配。)
如果这是真的,则只需更改此行:
.
为此:
在 Greasemonkey 中,由于沙箱保护,您无法以这种方式设置事件处理程序。请参阅Greasemonkey 中的常见陷阱。
此外,使用 jQuery,有更简单的方法来选择该元素。
像这样的东西:
$("#idname option:first").select (YourEventHandler)
应该可以工作。其中:
方便的 jQuery 参考。
Update:
Ok, if I understand the revised question, the code now works as intended except when option 1 starts as selected. (PS, the id's of the given code should be edited to match up.)
If that's true, then just change this line:
.
To this:
In Greasemonkey, you can't set event handlers that way due to sandbox protection. See Common Pitfalls in Greasemonkey.
Also, with jQuery, there are easier ways to select that element.
Something like:
$("#idname option:first").select (YourEventHandler)
should work.Where:
Handy jQuery reference.
您可以使用
.selectedIndex
属性 来获取选定的选项,如下所示:它是一个基于 0 的索引,因此 0 是示例标记中的
选项 1
,< a href="http://jsfiddle.net/t73bN/" rel="nofollow noreferrer">您可以在这里测试/使用它。我不清楚这个问题,但如果你想在
change
事件中这样做,它看起来像这样:您无法在此处测试。
You can use the
.selectedIndex
property to get which one's selected, like this:It's a 0-based index, so 0 is
Option 1
in your example markup, you can test/play with it here.I'm unclear from the question, but if you want this on the
change
event, it'd look like this:You cant test it here.