-1

我必须刷新页面才能运行我的脚本。需要对我的代码执行什么操作才能使其在不刷新的情况下正常工作?

HTML

                    <div id="lblSoru" align="center" style="position: relative; top: 100px; font-size:20px; font-family: proxima_nova_rgregular; color:" class="font">Which country is bigger than another</div>     

        <td><input id="rdCevaplar_0" type="radio" name="rdCevaplar" value="1" onclick="javascript:setTimeout(&#39;__doPostBack(\&#39;rdCevaplar$0\&#39;,\&#39;\&#39;)&#39;, 0)" /><label for="rdCevaplar_0">France</label></td>
    </tr><tr>
        <td><input id="rdCevaplar_1" type="radio" name="rdCevaplar" value="2" onclick="javascript:setTimeout(&#39;__doPostBack(\&#39;rdCevaplar$1\&#39;,\&#39;\&#39;)&#39;, 0)" /><label for="rdCevaplar_1">Germany</label></td>
    </tr><tr>
        <td><input id="rdCevaplar_2" type="radio" name="rdCevaplar" value="3" onclick="javascript:setTimeout(&#39;__doPostBack(\&#39;rdCevaplar$2\&#39;,\&#39;\&#39;)&#39;, 0)" /><label for="rdCevaplar_2">Romania</label></td>
    </tr><tr>
        <td><input id="rdCevaplar_3" type="radio" name="rdCevaplar" value="4" onclick="javascript:setTimeout(&#39;__doPostBack(\&#39;rdCevaplar$3\&#39;,\&#39;\&#39;)&#39;, 0)" /><label for="rdCevaplar_3">Greece</label></td>
    </tr>

油猴代码:

// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js

 var answerKey   = [
      { q: "Which country is bigger than another", a: "Germany" }
    , { q: "Best SciFi franchise", a: "Star Trek" }
    , { q: "Most badass monster", a: "Bun-Bun" }
    , { q: "Most toxic chemical in this list", a: "Mountain Dew" }
    // etc.
];

//--- Loop through the question(s) on the page and answer then if possible.
var questionTxt = $("#lblSoru");

questionTxt.each ( function () {
    var bFoundAnswer    = false;
    var answerTxt       = getAnswer (this.textContent);


    if (answerTxt) {
        //--- We have the answer text, now find the matching radio button and select it.
        var ansForThisQ     = $("label");
        console.log ("getAnswer: ", ansForThisQ);

        ansForThisQ.each ( function () {
            var zRegExp     = new RegExp (answerTxt, 'i');
            if (zRegExp.test (this.textContent) ) {
                bFoundAnswer    = true;
                var label       = $(this);
                var radioButt   = $("#" + label.prop ("for") );
                radioButt.prop  ("checked", "checked");
                label.css       ("background", "lime");
                return false;   // End loop
            }
        } );
    }
    else {
        alert ("I don't know how to answer: '" + this.textContent + "'");
        $(this).css ("background", "pink");
    }
    if ( answerTxt  &&  ! bFoundAnswer) {
        alert ("The page does not have the specified answer for the question: '" + this.textContent + "'");
        $(this).css ("background", "pink");
    }
} );

function getAnswer (questionText) {
    for (var J = answerKey.length - 1;  J >= 0;  --J) {
        var zRegExp = new RegExp (answerKey[J].q, 'i');

        if (zRegExp.test (questionText) )
            return answerKey[J].a;
    }
    return null;
}
4

1 回答 1

-1

// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js

var answerKey = [
    { q: "Which country is bigger than another", a: "Germany" }, 
    { q: "Best SciFi franchise", a: "Star Trek" }, 
    { q: "Most badass monster", a: "Bun-Bun" }, 
    { q: "Most toxic chemical in this list", a: "Mountain Dew" }
];
var $lblSoru = $('#lblSoru');
var answerTxt = getAnswer($lblSoru.text());
if(answerTxt) {
    var bFoundAnswer = false;
    $('label').each(function() {
        if(!bFoundAnswer && $(this).text().toLowerCase() == answerTxt.toLowerCase()) {
            $('#' + $(this).attr('for')).attr('checked', 'checked');
            bFoundAnswer = true;
        }
    });
    if(!bFoundAnswer) {
        alert ("The page does not have the specified answer for the question: '" + $lblSoru.text() + "'");
        $lblSoru.css('background', 'pink');
    }
}
else {
    alert("I don't know how to answer: '" + $lblSoru.text() + "'");
    $lblSoru.css('background', 'pink');
}

function getAnswer(questionText) {
    for(var i = 0; i < answerKey.length; i++) {
        if(answerKey[i].q.toLowerCase() == questionText.toLowerCase()) {
            return answerKey[i].a;
        }
    }
    return null;
}

工作演示

顺便说一句,你错过了<tr>after Which country is bigger than another</div>

于 2013-10-26T10:29:15.710 回答