Tampermonkey选择一个下拉值.它没有ID或名称,只是一个类? [英] Tampermonkey to select a dropdown value. It has no ID or name, just a class?
问题描述
我正在为 ConverTo :
应该会自动在下拉菜单中选择第二个选项:
I'm writing a Tampermonkey code for a webpage like ConverTo:
It's supposed to automatically select the second option in a dropdown:
<select class="format-select">
<option value="mp3">MP3</option>
<option value="mp4">MP4 (video)</option>
</select>
,页面完全加载后几秒钟.
但是什么也没发生.
, several seconds after the page is fully loaded.
But nothing happens.
我的代码:
......
// @match https://www.converto.io/*
// @require http://code.jquery.com/jquery-1.11.0.min.js
// ==/UserScript==
$(document).ready(function(){
setTimout(test(),10000);
function test()
{
$(".format-select").val('mp4');
}
})();
请帮助!
推荐答案
See Choosing and activating the right controls on an AJAX-driven site.
Many AJAX-driven controls cannot just be changed; they also must receive key events, for the page to set the desired state.
在 ConverTo 情况下,该选择似乎期望‡:
In the ConverTo case, that select appears to expect‡ :
- 点击事件.
- 价值变化.
- 变更事件.
您将使用以下完整,有效的脚本代码发送该序列:
You would send that sequence with code like this complete, working script:
// ==UserScript==
// @name _ConverTo, Automatically select mp4
// @match https://www.converto.io/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==
//- The @grant directive is needed to restore the proper sandbox.
waitForKeyElements (".format-select:has(option[value=mp4])", selectFinickyDropdown);
function selectFinickyDropdown (jNode) {
var evt = new Event ("click");
jNode[0].dispatchEvent (evt);
jNode.val('mp4');
evt = new Event ("change");
jNode[0].dispatchEvent (evt);
}
‡到同一末端还有其他状态序列.
‡ There are other state sequences possible, to the same end.
这篇关于Tampermonkey选择一个下拉值.它没有ID或名称,只是一个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!