-1

我想用 Greasemonkey 选择一些文本突出显示。当我看到四个单选按钮时,我需要 Greasemonkey 突出显示我的选项。示例:我想要自动选择标签 RealMadrid。

        <div class="row">
              <input type="radio" tabindex="3" value="answer1" name="ctl00$ContentPlaceHolder1$1" id="ContentPlaceHolder1_answer1"><label for="ContentPlaceHolder1_answer1">Bayern</label></div>

<div class="row">
            <input type="radio" tabindex="4" value="answer2" name="ctl00$ContentPlaceHolder1$1" id="ContentPlaceHolder1_answer2"><label for="ContentPlaceHolder1_answer2">Barcelona</label></div>

<div class="row">
            <input type="radio" tabindex="5" value="answer3" name="ctl00$ContentPlaceHolder1$1" id="ContentPlaceHolder1_answer3"><label for="ContentPlaceHolder1_answer3">RealMadrid</label></div>

<div class="row">
            <input type="radio" tabindex="6" value="answer4" name="ctl00$ContentPlaceHolder1$1" id="ContentPlaceHolder1_answer4"><label for="ContentPlaceHolder1_answer4">Lyon</label></div>

 </div>

Greasemonkey 代码:(已选中收音机,但我需要突出显示文本)可能吗?

var labels = document.getElementsByTagName('label'); //get the labels
for (var i = 0; i < labels.length; ++i) { //loop through the labels
    if (labels[i].textContent == "RealMadrid") { //check label text
        labels[i].click(); //if correct text, click the label
    }
}
4

1 回答 1

0

Javascript:

var radioButtons = document.querySelectorAll('input[type=radio]');
for(var i = 0; i < radioButtons.length; i++) {
    radioButtons[i].addEventListener('change', changeHandler, false);
}
function changeHandler(event) {
    var b = event.target;
    if(b.checked) {
        document.querySelector('label[for=' + b.id + ']').style.background = 'yellow';
        var notB = document.querySelectorAll('label:not([for=' + b.id + '])');
        for(var j = 0; j < notB.length; j++) {
            notB[j].style.background = '';
        }
    }
}

jQuery:

$(':radio').change(function() {
    if($(this).is(':checked')) {
        $(this).next().css('background', 'yellow');
        $(':radio').not(this).next().css('background', '');
    }
});

演示

于 2013-10-26T06:59:09.273 回答