I originally wanted to determine which cells were selected, but it was found that they did not work. I could only judge which cells passed after the mouse was pressed.
The reason I posted it is that I think there are many interesting things in the case.
The continuous trigger of onmouseover causes many duplicate elements
Since the event is bound to the entire table, undefined also appears
The repeated entry and exit of the mouse will cause intermittent repetition of the same element.
How to solve them!
<table id="dnf"> <tr> <td rowspan="2">2</td> <td>2</td> <td>4</td> </tr> <tr> <td>2</td> <td>4</td> </tr> <tr> <td>2</td> <td>4</td> </tr> </table>
window.onload = function() { var flag = false;<BR> //When the mouse is pressed, it is true, and letting go is true var indexs =[];<BR> //Useed to store the cell passing by the mouse in the entire table position, and is initialized when the mouse is pressed, dnf.onmousedown = function() { flag = true; indexs = []; } dnf.onmousemove = function(e) { if(flag)//Only when the mouse is pressed, the compound code will be executed { indexs.push(search(e.target,dnf.getElementsByTagName("td"))) } } dnf.onmouseup = function() { flag = false; deleteUndefined();//Because it passes through the border, the null element deleteRepaint() will appear;//Because onmouseover will not only trigger once, and we choose to pass through a cell repeatedly alert(indexs); } function deleteRepaint() { for(var j=0;j<indexs.length;j++)//Ensure that the jth element is unique { var head = indexs[j]; for(var i=j+1;i<indexs.length;i++)//Delete the jth duplicate { if(head == indexs[i]) { indexs.splice(i,1); i--; } } } } function deleteUndefined() { for(var i=0;i<indexs.length;i++) { if(typeof indexs[i] == "undefined") { indexs.splice(i,1); i--; } } } function search(a,A) { var length = A.length; for(var i=0;i<length;i++) { if(a == A[i]) { return i; } } } } } </script>The above original js article, after pressing the mouse, passed through those cells, is the simple example of the editor sharing with you. I hope you can give you a reference and I hope you can support Wulin.com more.