dongxi7704 2014-01-21 12:56
浏览 26
已采纳

识别第二次鼠标单击网格中的同一单元格

i want to place an image inside the cell on the first click and another image on the second click at the same position. In order to do that i tried writing onclick function in the tag..but as its written within functions it wont work..is there any other way to achieve the need ? plz help?

JS for placing the images on click:-

function changeColor1(elem)
{ 
     elem.innerHTML = '<img src="images/atom1.png" width="20px" height="20px" onclick=changecolor2(this) />';   
    //alert("Row Index is:"+elem.parentNode.rowIndex+elem.cellIndex);
    var x= elem.cellIndex;
    var y= elem.parentNode.rowIndex;
    click_count(x,y);
 }
 function changeColor2(elem)
{ 
     elem.innerHTML = '<img src="images/2circles.png" width="20px" height="20px" onclick=changecolor3(this) />';    
    //alert("Row Index is:"+elem.parentNode.rowIndex+elem.cellIndex);
    var x= elem.cellIndex;
    var y= elem.parentNode.rowIndex;
    click_count(x,y);
 }

code for making the grid:

$rows = 7; // define number of rows
$cols = 6;// define number of columns
echo "<center>";
echo "<table border='1'   cellspacing=0 cellpadding='35' pixels'>";

for($tr=0;$tr<$rows;$tr++){

    echo "<tr>";
        for($td=0;$td<$cols;$td++){
               echo "<td onclick=\"changeColor1(this)\" >&nbsp &nbsp</td>";
        }
    echo "</tr>";
  • 写回答

1条回答 默认 最新

  • dptgpyl61857413 2014-01-21 13:01
    关注

    Try this:

    function hasClass(element, cls) {
    return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1;
    }
    
    function changeColor2(elem)
    { 
     if(!hasClass(elem, 'clicked')){
      //clicked first time 
       elem.className = elem.className + " clicked";//add clicked class to element on first click
       elem.innerHTML = '<img src="images/2circles.png" width="20px" height="20px"    onclick=changecolor3(this) />';    
      //alert("Row Index is:"+elem.parentNode.rowIndex+elem.cellIndex);
      var x= elem.cellIndex;
      var y= elem.parentNode.rowIndex;
      click_count(x,y);}
    else{
        //clicked second time and onward
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?