代码之家  ›  专栏  ›  技术社区  ›  Max Abrahamsson

在img标记中添加一个新值来检查它是否被Javascript点击?

  •  0
  • Max Abrahamsson  · 技术社区  · 16 年前

    我正在尝试用重复的图像制作一个地图,我还希望地图上的瓷砖可以点击。在这里,我写了一个脚本,但我有问题,取消选中的图像。

    http://jsbin.com/aruge/edit

    请帮忙。

    2 回复  |  直到 16 年前
        1
  •  1
  •   user372551 user372551    16 年前

    以下是jQuery版本的需求 : http://jsbin.com/utepi3

      $(function() {
        var counter = 0;
        for(var i = 0;i< 5; i++) {
          for(var k = 0; k < 5; k++) {
            var img = $('<img />', {
              id : 'i'+i+'k'+k,
              src: 'http://mclightning.com/gr.jpg',
              width : 30,
              height : 30,
              alt : 'test',
              value : '0'
            }).appendTo('body').wrap('<div class="hello"/>');
            $('.hello').eq(counter).css({
              position : 'absolute',
              left: i*30,
              top : k*30,
              cursor : 'pointer'
            });
            counter++;
          }
        }
        $('img').hover(function() {
          if($(this).attr('value') != 1) {
            $(this).attr('src', 'http://mclightning.com/grover.jpg');
          }  
        },function() {
          if($(this).attr('value') != 1) {
            $(this).attr('src', 'http://mclightning.com/gr.jpg');
          }  
        }).bind('click',function() {
          if($(this).attr('value') !='1' ) {
            $(this).attr('src', 'http://mclightning.com/grclick.jpg');
            $(this).attr('value','1');
          }else if($(this).attr('value') !='0' ) {
            $(this).attr('src', 'http://mclightning.com/gr.jpg');
            $(this).attr('value','0');
          }  
        });
      });  
    

    附言

        2
  •  1
  •   trrrrrrm    16 年前

    我不知道你到底想要什么,但那会有帮助吗

    function gotover(val) {
        if(document.getElementById(val).st!='1') {
      x=val.substr(1,1)*30;
      y=val.substr(3,1)*30;
          document.getElementById(val).src="http://mclightning.com/grover.jpg";
        }
    }
    function gotout(val) {
      if(document.getElementById(val).st!='1') {
      x=val.substr(1,1)*30;
      y=val.substr(3,1)*30;
      document.getElementById(val).src="http://mclightning.com/gr.jpg";
      }
    }
    function gotclicked(val) {
      alert(document.getElementById(val).alt);
    }
      var x=0;
      var y=0;
      for(i=0; i<5; i++)
      {
      x=i*30;
        for(k=0; k<5; k++)
        {
          y=k*30;
        document.write('<div class="kutu" style="position: absolute; left: '+x+'px; top: '+y+'px;"><img  onmouseover="gotover(this.id)" onclick="gotclicked(this.id)" onmouseout="gotout(this.id)" id="i'+i+'k'+k+'" src="http://mclightning.com/gr.jpg" width="30" height="30" alt="test"></div>');
        }
      }