Jump to content
Fórum Script Brasil
  • 0

Duvida de evento em tabela via javascript


shakall

Question

Olá pessoal!

Estou fazendo uma tabela que ao clicar em uma célula ela muda a cor de fundo com o código abaixo consegui fazer esta parte, agora o problema é que quando eu clicar e arrastar ela tem que mudar a cor de todas as células onde o mouse passar.

Alguém pode me ajudar, como fazer para que minha função de mudar a cor da célula seja chamada ao clicar e arrastar o mouse, todas as células por onde ele passar seja trocada as cores?

<script>
    function mudaCor(id) {
        if (document.getElementById(id).style.background == "none repeat scroll 0% 0% white") {
            document.getElementById(id).style.background = "green";
        } else {
            document.getElementById(id).style.background = "white";
        }
    }
</script>
<table  border="1" align="center">
    <tr>
        <td id="tdCor1" onmousedown="java script:mudaCor('tdCor1')" width="15" height="15" style="background:white"></td>
        <td id="tdCor2" onmousedown="java script:mudaCor('tdCor2')" width="15" height="15" style="background:white"></td>
        <td id="tdCor3" onmousedown="java script:mudaCor('tdCor3')" width="15" height="15" style="background:white"></td>
        </tr>
    <tr>
        <td id="tdCor25" onmousedown="java script:mudaCor('tdCor25')" width="15" height="15" style="background:white"></td>
        <td id="tdCor26" onmousedown="java script:mudaCor('tdCor26')" width="15" height="15" style="background:white"></td>
        <td id="tdCor27" onmousedown="java script:mudaCor('tdCor27')" width="15" height="15" style="background:white"></td>
        </tr>
</table>

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Não é isso que você quer, mas vê se te dá uma luz.

var novaCor = false;

function mudaCorClick(id) {
    if (document.getElementById(id).style['background-color'] == "white") {
        document.getElementById(id).style['background-color'] = "green";
        novaCor = "green";
    } else {
        document.getElementById(id).style['background-color'] = "white";
        novaCor = "white";
    }
}

function mudaCorOver(objeto) {
    if (!novaCor) return 0;
    if (objeto.style['background-color'] != novaCor) {
        objeto.style['background-color'] = novaCor;
    }
}

function saiMouse() {
    novaCor = false;
}
<table  border="1" align="center">
    <tr>
        <td id="tdCor1" onmousedown="java script:mudaCorClick('tdCor1')" onmouseover="java script:mudaCorOver(this)" onmouseup="java script:saiMouse()" width="15" height="15" style="background-color:white"></td>
        <td id="tdCor2" onmousedown="java script:mudaCorClick('tdCor2')" onmouseover="java script:mudaCorOver(this)" onmouseup="java script:saiMouse()" width="15" height="15" style="background-color:white"></td>
        <td id="tdCor3" onmousedown="java script:mudaCorClick('tdCor3')" onmouseover="java script:mudaCorOver(this)" onmouseup="java script:saiMouse()" width="15" height="15" style="background-color:white"></td>
        </tr>
    <tr>
        <td id="tdCor25" onmousedown="java script:mudaCorClick('tdCor25')" onmouseover="java script:mudaCorOver(this)" onmouseup="java script:saiMouse()" width="15" height="15" style="background-color:white"></td>
        <td id="tdCor26" onmousedown="java script:mudaCorClick('tdCor26')" onmouseover="java script:mudaCorOver(this)" onmouseup="java script:saiMouse()" width="15" height="15" style="background-color:white"></td>
        <td id="tdCor27" onmousedown="java script:mudaCorClick('tdCor27')" onmouseover="java script:mudaCorOver(this)" onmouseup="java script:saiMouse()" width="15" height="15" style="background-color:white"></td>
        </tr>
</table>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...