-2

我有一个任务来创建一个拼图。我有 12 个 div。每个 div 包含最多 12 个不同的值。我有像现在开始这样的按钮。我想在每次单击立即开始按钮时更改我的 div 值。 演示

<div id="container">
        <div class="finder">
            <div class="block1">    <div id="one"><h1>1</h1></div>
            </div>
            <div class="block1">    <div id="two"><h1>2</h1></div>
            </div>
            <div class="block1">    <div id="three"><h1>3</h1></div>
            </div>
            <div class="block1">    <div id="four"><h1>4</h1></div>
            </div>
            <div class="block1">    <div id="five"><h1>5</h1></div>
            </div>
            <div class="block1">    <div id="six"><h1>6</h1></div>
            </div>
            <div class="block1">    <div id="seven"><h1>7</h1></div>
            </div>
            <div class="block1">    <div id="eight"><h1>8</h1></div>
            </div>
            <div class="block1">    <div id="nine"><h1>9</h1></div>
            </div>
            <div class="block1">    <div id="ten"><h1>10</h1></div>
            </div>
            <div class="block1">    <div id="eleven"><h1>11</h1></div>
            </div>
            <div class="block1">    <div id="twelve"><h1>12</h1></div>
            </div>

        </div>
        <div class="button">
            <div id="startButton">
                <button>Start</button>
            </div>
        </div>
    </div>

js是

 $(".finder").css('display','none');
      $("#startButton").click(function () {
        $(".finder").css('display','block');
      });
      var myDiv;
      $(".block1 div").on('click', function(){myDiv = $(this)});
      $(".block1 div").click(function () {
        if(myDiv.text() < 12)
        {
            myDiv.text(parseInt(myDiv.text())+1);
        }
        if(myDiv.text() > 12)
        {
            myDiv.text(1);
            myDiv.text(parseInt(myDiv.text())+1);
        }
      });

我怎样才能改变价值观?

4

2 回答 2

4

我在下面提供了一个解决方案。您应该从中获得的关键要点之一是,当使用事件处理程序时,this将为绑定函数中触发事件的元素分配。

要使用随机数填充框,请使用此代码

$(function() {
    //Array of values
    var values = [1,2,3,4,5,6,7,8,9,10,11,12];

    $( "input[type=submit], button" )
      .button()
      .click(function( event ) {
        event.preventDefault();
      });
      $(".finder").css('display','none');
      $("#startButton").click(function () {

          var tmpValues = values.slice(); //copy array


          $(".block1 div h1").each(function(){
               var r = Math.floor(Math.random()*tmpValues.length);
               //assign html to random array value that gets removed from array
               $(this).html(tmpValues.splice(r,1));   
          });
        $(".finder").css('display','block');
      });

      $(".block1 div").click(function () {

        //Instead of setting myDiv use $(this) which will refer to the div
        //We can grab the value of the H1 within the div using a scoped selector
        var value = parseInt($("h1",this).html());


        //Set the html of the clicked div
        //When setting the html we must include new value in h1 to maintain style
        //Ternary expression tidies up the logic, not sure why it sets to to 2 though?
        $(this).html("<h1>" + ((value > 12) ? 2: value+1) + "</h1>");
      });
  });

工作示例 http://jsfiddle.net/W55xc/3/

于 2013-07-02T09:20:05.597 回答
1

我不太明白这个问题,但我猜你想为 div生成随机数,对吧?利用Math.random()

$(".block1 div").each(function() {
  $(this).children('h1').text(Math.floor((Math.random() * 12) + 1)); // Generate a random number <-> [1, 12]
}
于 2013-07-02T09:14:16.517 回答