我必须刷新页面才能运行我的脚本。我的代码需要做些什么才能让它无需刷新?
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('__doPostBack(\'rdCevaplar$0\',\'\')', 0)" /><label for="rdCevaplar_0">France</label></td>
</tr><tr>
<td><input id="rdCevaplar_1" type="radio" name="rdCevaplar" value="2" onclick="javascript:setTimeout('__doPostBack(\'rdCevaplar$1\',\'\')', 0)" /><label for="rdCevaplar_1">Germany</label></td>
</tr><tr>
<td><input id="rdCevaplar_2" type="radio" name="rdCevaplar" value="3" onclick="javascript:setTimeout('__doPostBack(\'rdCevaplar$2\',\'\')', 0)" /><label for="rdCevaplar_2">Romania</label></td>
</tr><tr>
<td><input id="rdCevaplar_3" type="radio" name="rdCevaplar" value="4" onclick="javascript:setTimeout('__doPostBack(\'rdCevaplar$3\',\'\')', 0)" /><label for="rdCevaplar_3">Greece</label></td>
</tr>
Greasemonkey代码:
// @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;
}
答案 0 :(得分:-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>
之后你错过了Which country is bigger than another</div>
。