我想选择带Greasemonkey的自动单选按钮。但我想选择标签名称的收音机。 当我看到四个单选按钮时,Greasemonkey需要选择我的选项。 示例:我想要自动选择标签 Instagram 。
<label for="ContentPlaceHolder1_answer3">Instagram</label>
如何使用Greasemonkey选择此单选按钮?
<div class="row">
<input type="radio" tabindex="3" value="answer1" name="ctl00$ContentPlaceHolder1$1" id="ContentPlaceHolder1_answer1"><label for="ContentPlaceHolder1_answer1">Twitpic</label></div>
<div class="row">
<input type="radio" tabindex="4" value="answer2" name="ctl00$ContentPlaceHolder1$1" id="ContentPlaceHolder1_answer2"><label for="ContentPlaceHolder1_answer2">Yfrog</label></div>
<div class="row">
<input type="radio" tabindex="5" value="answer3" name="ctl00$ContentPlaceHolder1$1" id="ContentPlaceHolder1_answer3"><label for="ContentPlaceHolder1_answer3">Instagram</label></div>
<div class="row">
<input type="radio" tabindex="6" value="answer4" name="ctl00$ContentPlaceHolder1$1" id="ContentPlaceHolder1_answer4"><label for="ContentPlaceHolder1_answer4">Flickr</label></div>
</div>
答案 0 :(得分:1)
试试这个:
var label="whatever"; //The label you want to look for, you may already have some such variable
var allLabels=document.getElementsByTagName("label"); //Get all labels
for (var i=0;i<allLabels.length;i++) { //Loop through labels
if (allLabels[i].innerHTML.toLowerCase()==label) { //If a match is found...
allLabels[i].parentNode.getElementsByTagName("input")[0].checked=true; //Check the parent element for inputs (radio buttons, for instance), and check the first one
}
}
如果不知道如何将HTML放在一起,我无法确定这是否有效。您在问题中提供了HTML,但我不知道它是否保留了这种常量格式。
答案 1 :(得分:1)
我暂时没有使用过xpath,所以它可能什么都不做:)
但理论上,这将搜索文本构成“Instagram”的所有标签,然后循环遍历所有结果,使用for属性获取单选按钮并将其设置为true。 (虽然我认为它的“选择”用于单选按钮?)
function xpathSnapshot(expression,context) {
var r=document.evaluate(expression,context,null,6,null),i=r.snapshotLength-1;
this.iterate=function() {return r.snapshotItem(i--)}
}
var labels = xpathSnapshot("//label[contains(text(),\"Instagram\")]",document);
while (label = labels.iterate()) {
document.getElementById(label.getAttribute("for")).checked = true;
}
答案 2 :(得分:0)
var labels = document.getElementsByTagName('label'); //get the labels
for (var i = 0; i < labels.length; ++i) { //loop through the labels
if (labels[i].textContent == "Instagram") { //check label text
labels[i].click(); //if correct text, click the label
}
}