我希望浏览器能够以绿色突出显示问题的正确答案 我发现an answer that seems to be the right idea (How can I automatically select specific radio buttons with Greasemonkey?)但我不知道使用它的javascript。
我的HTML代码显示在this fiddle中。
所需的输出示例应如下所示:
我如何根据自己的情况调整其他答案?
答案 0 :(得分:0)
var questionTxt =
和var ansForThisQ =
行必须适应目标网页的特定HTML。
同样,ansForThisQ.each (...
循环中的代码也必须定制。
鉴于您的示例HTML具有以下结构:
<div class="soru-content">
<div class="point" style="margin-right:0px">
<span><img src="IMG_URL"/></span>
</div>
<div class="soru">
<p>How many countries will participate in the games?</p>
<ul>
<li><a href="ANS_URL1"><span>12</span></a></li>
<li><a href="ANS_URL2"><span>204</span></a></li>
<li><a href="ANS_URL3"><span>552</span></a></li>
<li><a href="ANS_URL4"><span>715</span></a></li>
</ul>
</div>
</div>
然后改编的脚本代码将是:
var answerKey = [
{ q: "How many countries will participate in the games?", a: "204" }
, { q: "when do olympics start in london", a: "July 27" }
// etc.
];
//--- Loop through the question(s) on the page and answer then if possible.
var questionTxt = $("div.soru-content div.soru p");
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 = $(this).parent ().find ("ul a span");
ansForThisQ.each ( function () {
var zRegExp = new RegExp (answerTxt, 'i');
if (zRegExp.test (this.textContent) ) {
bFoundAnswer = true;
$(this).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;
}
有关jQuery选择器的更多信息,请参见this part of the doc。