简体   繁体   English

在“列表框”中选择“ li”元素(JavaScript)

[英]Select `li` element in `listbox` (JavaScript)

I want to present https://translate.yandex.com/m/translate page to user with preselected source and destination language. 我想将https://translate.yandex.com/m/translate页面呈现给具有预先选择的源语言和目标语言的用户。 I can access to interesting element like this 我可以访问像这样的有趣元素

var srcLangContent = document.getElementById("srcLanguagesContent")
var liToSelect = srcLangContent.children[10]

What to do next I don't know. 下一步该怎么办,我不知道。 I called click() , but it's not working. 我叫click() ,但是它不起作用。 Can you give some advice? 你能给点建议吗?

PS I can't use jQuery PS我不能使用jQuery

Here is solution. 这是解决方案。

var mouse_down_evt = document.createEvent("MouseEvents");
mouse_down_evt.initMouseEvent("mousedown", true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null);

var mouse_up_evt = document.createEvent("MouseEvents");
mouse_up_evt.initMouseEvent("mouseup", true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null);

liToSelect.dispatchEvent(mouse_down_evt)
liToSelect.dispatchEvent(mouse_up_evt)

Thank you @PhilCooper for advice. 谢谢@PhilCooper的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM