[英]clicking select element and then clicking an option
I'm creating a chrome extension that will simulate a click on a drop down element in the dom, and then click again on one of the options, according to the option I will pass as an argument. 我正在创建一个chrome扩展,它将模拟dom中下拉元素的单击,然后根据我将作为参数传递的选项再次单击其中一个选项。 To clarify: I am not waiting for a click from the user, but want to create it myself, using the chrome extension. 澄清:我不是在等待用户点击,而是想自己创建,使用chrome扩展。 How can it be achieved? 如何实现? I didn't find any way. 我没有找到任何办法。 This is the relevant part of the html: 这是html的相关部分:
<select class="select select-step-type" name="select-step-type" data-rel="tooltip" title="" data-original-title="Edit Step">
<optgroup label="Select a step type">
<option value="warmup" data-step-type="workout-step-warm" class-name="">
Warm up
</option>
<option value="interval" data-step-type="interval" class-name="workout-step-run" selected="">
Run
</option>
<option value="recovery" data-step-type="workout-step-recover" class-name="">
Recover
</option>
<option value="rest" data-step-type="workout-step-rest" class-name="">
Rest
</option>
<option value="cooldown" data-step-type="workout-step-cool" class-name="">
Cool down
</option>
<option value="other" data-step-type="workout-step-other" class-name="">
Other
</option>
</optgroup>
And this it how I thought to do it in js (ignore the theHackCode, it simply gathers the code to be executed on the page with the chrome extension). 这就是我在js中的想法(忽略theHackCode,它只是收集要在chrome扩展名的页面上执行的代码)。
var theHackCode = "document.getElementsByClassName('select-step-type')[0].click(); ";
theHackCode += "document.getElementsByClassName('select-step-type')[0].options[4].click(); ";
chrome.tabs.executeScript(null, {code: theHackCode}, function(results) {
console.log(results);
});
You need to try this,it will alter you label and value of droopdown. 你需要试试这个,它会改变你的标签和下垂的价值。
document.addEventListener('DOMContentLoaded', function () {
var obj = document.getElementsByClassName('select-step-type');
for (var i = 0; i < obj.length; i++) {
obj[i].addEventListener('change', function(){
alert(this.options[this.selectedIndex].value+"\n"+this.options[this.selectedIndex].text);
}, false);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.