我怎么能用greasemonkey突出显示一些文字?

时间:2013-10-23 23:15:58

标签: greasemonkey

我想用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
    }
}

1 个答案:

答案 0 :(得分: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', '');
    }
});

Demo