English 中文(简体)
Javascript - Hidden maths response.
原标题:Javascript - Hidden maths answer.

以下文字生成随机数字,计算答案。

下一步是,我要回答丹麦难民保护局,并在那里放置一个文本区。 然后,你们必须拿出答案,如果答案正确,那么答案就应当绿色。

我知道这样做是可能的,但我却在互联网上找不到成功,因此我会提出问题。

页: 1

<div id="breuken"></div>

$(function () {
    var number = document.getElementById("breuken");
    var i = 0;
    for (i = 1; i <= 10; i++) {
        var sRandom = Math.floor(Math.random() * 10);
        var fRandom = Math.floor(sRandom + Math.random() * (10 - sRandom));
        var calc = Math.abs(fRandom - sRandom);
        number.innerHTML += "" + fRandom + " - " + sRandom + " = " + calc + "<br />";
    }
    number.innerHTML;
});
最佳回答

这一答复实际上与@gillesc s,但使用j 做重提的金字标/代码,如果你有的话,你应当使用。 (缩短)

Javascript Code: jsFiddle

$(function() {
    var checkAnswer = function(elem) {
        if ($(elem).data( calc ) == $(elem).val()) {
            $(elem).css( background-color ,  green );
        } else {
            $(elem).css( background-color ,  white );
        }
    };
    var div = $( #breuken );

    for (var i = 0; i < 10; i++) {
        var sRandom = Math.floor(Math.random() * 10);
        var fRandom = Math.floor(sRandom + Math.random() * (10 - sRandom));
        var calc = Math.abs(fRandom - sRandom);

        var qa = $( <span>  + fRandom +   -   + sRandom + 
              = <input type="text"></span><br /> );
        qa.find( input ).change(function() {
            checkAnswer(this);
        }).data( calc , calc);
       div.append(qa);
    }
});
问题回答

Try this http://jsfiddle.net/r4QTQ/

它确实是基本的绿色改变,但应当足以使你改变和正确改变你们想要的东西。

创立了这一j Query demo,如果你使用j Query,可能有助于你正确方向。

var $questions = $( <div /> ).attr( id ,  questions );
for (var i = 1; i <= 3; i++) {

    var question = "Question " + i + "?";
    var answer = i;

    $questions.append(
        $( <div /> )
            .attr( id ,  question  + i)
            .addClass( question )
            .append(
                $( <span /> ).text(question)
            )
            .append(
                $( <input type="text" /> )
                    .addClass( answer )
                    .data( answer , answer)
            )
    );
}
$( #container ).empty().append($questions);

希望!





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!