简体   繁体   中英

Move html td element using JavaScript

I am currently creating a basic javascript car racer whereby the car is a an HTML tr element placed in a td. I want to have it that when I push the key "e" player one will move and if the "d" key is pressed player 2 will move. I want to do this by using an eventListener to have it that when the key is pressed the car will move from its original td element to the next td element and remove the car from the previous. This will make it look like the car has moved. I do not know how to have this element moved using the key press but my code is below. Thanks for the help!

  document.addEventListener('DOMContentLoaded', function() { //run the code function move = function(player){ /* psuedo code: if Keypressed = "E" then move first car forward else d move second car forward if player1 has moved to the end then end the game else for player 2 set a var to add up the total moves, user can then easily adapted the length of the road and have it match the var total. */ } function keyPress = function(e){ if (e.charCode == "e"){ //move player one } else if (e.charCode == "d"){ //move player 2 } else{ alert("Invalid key stroke"); } } }) 
 .racer_table td { background-color: white; height: 50px; width: 50px; border: 5px; border-color: black; } .racer_table td.active { background-color: black; } 
  <link rel="stylesheet" type = "text/css" href="style.css"> <script type="text/javascript" src="racer.js"> </script> <body> <table class="racer_table"> <tr id="player1_strip"> <td class="active"></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr id="player2_strip"> <td class="active"></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> </table> </body> </html> 

I would set a var with the object then use jquery's 'next' function for sibling object. if you add this button <button id="x1"/> this will work as an example:

$("#x1").click(function() {
            var active = $("#player1_strip").children(".active");
              active.next().addClass("active");
              active.removeClass("active");
    });

No jQuery solution. There is probably some other solution without need for an id, but I think this one is cleaner. You add an id to each <td> like 'tile1e', 'tile2e' etc. Then, your keypress function would look like this:

function keyPress = function(e){
    if (e.charCode == "e"){
        var tile = document.querySelector("#player1_strip > .active");  // get player tile
        var nextTile = 'tile' + (tile.id[4] + 1) + 'e';  // get id of the next tile
        tile.className = "";
        document.getElementById(nextTile).className = "active";

    } 
    else if (e.charCode == "d"){
        var tile = document.querySelector("#player2_strip > .active");  // get player tile
        var nextTile = 'tile' + (tile.id[4] + 1) + 'd';  // get id of the next tile
        tile.className = "";
        document.getElementById(nextTile).className = "active";

    }
    else{
        alert("Invalid key stroke");
    }
}

and the table something like this

<table class="racer_table">
  <tr id="player1_strip">
    <td id='tile0e' class="active"></td>
    <td id='tile1e'></td>
    <td id='tile2e'></td> <!-- move active to the next empty td element -->
    <td id='tile3e'></td>
    <td id='tile4e'></td>
    <td id='tile5e'></td>
    <td id='tile6e'></td>
    <td id='tile7e'></td>
    <td id='tile8e'></td>
    <td id='tile9e'></td>
  </tr>
  <tr id="player2_strip">
    <td id='tile0d' class="active"></td>
    <td id='tile1d'></td>
    <td id='tile2d'></td>
    <td id='tile3d'></td>
    <td id='tile4d'></td>
    <td id='tile5d'></td>
    <td id='tile6d'></td>
    <td id='tile7d'></td>
    <td id='tile8d'></td>
    <td id='tile9d'></td>
  </tr>
</table>

I hope you get the idea behind this.

But feel free to use the jQuery solution if you are comfortable with that, it is so much simpler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM