单击时高亮显示单元格边框颜色,单击时改回其他颜色

Highlight cell border color when click, change back when click to others

本文关键字:单击 颜色 其他 边框 高亮 显示 单元格      更新时间:2023-09-26

我有下面的代码:

function highlight(cell){
		cell.style.borderColor = "red";
	}
	
	function originalColor(cell){
		cell.style.borderColor = "black";
	}
td{
		cursor: pointer;
	}
<table border="1">
	<tr>
	<td  onmousedown="highlight(this);" onblur="originalColor(this);">Cell 1</td>
	<td  onmousedown="highlight(this);" onblur="originalColor(this);">Cell 2</td>
	<td  onmousedown="highlight(this);" onblur="originalColor(this);">Cell 3</td>
	</tr>
	<tr>
	<td  onmousedown="highlight(this);" onblur="originalColor(this);">Cell 4</td>
	<td  onmousedown="highlight(this);" onblur="originalColor(this);">Cell 5</td>
	<td  onmousedown="highlight(this);" onblur="originalColor(this);">Cell 6</td>
	</tr>
	<tr>
	<td onmousedown="highlight(this);" onblur="originalColor(this);">Cell 7</td>
	<td onmousedown="highlight(this);" onblur="originalColor(this);">Cell 8</td>
	<td onmousedown="highlight(this);" onblur="originalColor(this);">Cell 9</td>
	</tr>
	</table>

当单击时,它会将边框高亮显示为红色,当单击另一个单元格时,它可能会变回黑色,但它不起作用,我尝试了onmouseuponmouseover,它不起我想要的作用。

我尝试过的技术是使用tabindex,它是有效的;但这可以突出显示一个单元格,如果我试图同时选择两个单元格,那就行不通了。有人能帮忙吗?


    var redNow = 1;
    function highlight(cell) {
        redNow == 1 ? redNow = 0 : redNow.style.borderColor = "black";
        redNow = cell;
        cell.style.borderColor = "red";
    }
    td {
    		cursor: pointer;
    	}
   <table border="1">
    <tr>
        <td onmousedown="highlight(this);">Cell 1</td>
        <td onmousedown="highlight(this);">Cell 2</td>
        <td onmousedown="highlight(this);">Cell 3</td>
    </tr>
    <tr>
        <td onmousedown="highlight(this);">Cell 4</td>
        <td onmousedown="highlight(this);">Cell 5</td>
        <td onmousedown="highlight(this);">Cell 6</td>
    </tr>
    <tr>
        <td onmousedown="highlight(this);">Cell 7</td>
        <td onmousedown="highlight(this);">Cell 8</td>
        <td onmousedown="highlight(this);">Cell 9</td>
    </tr>
</table>

在您的澄清评论之后,如果我理解正确,您希望在单击单元格时高亮显示它,如果您选择通过单击另一个元素来删除高亮显示。如果没有,请再次澄清。如果确实是这样的话,那么下面的代码将通过向上遍历DOM到父表,然后遍历所有单元格并采取适当的操作来工作:

function toggleBorderColor(c) {
    cells = c.parentElement.parentElement.getElementsByTagName('td');
    for (var i in cells) {
       var cell = cells.item(i);
       cell.style.borderColor = (cell != c) ? "" : "red";
    }
}
td {
	cursor: pointer;
}
<table border="1">
    <tr>
	<td  onmousedown="toggleBorderColor(this);">Cell 1</td>
	<td  onmousedown="toggleBorderColor(this);">Cell 2</td>
	<td  onmousedown="toggleBorderColor(this);">Cell 3</td>
    </tr>
    <tr>
	<td  onmousedown="toggleBorderColor(this);">Cell 4</td>
	<td  onmousedown="toggleBorderColor(this);">Cell 5</td>
	<td  onmousedown="toggleBorderColor(this);">Cell 6</td>
    </tr>
    <tr>
	<td onmousedown="toggleBorderColor(this);">Cell 7</td>
	<td onmousedown="toggleBorderColor(this);">Cell 8</td>
	<td onmousedown="toggleBorderColor(this);">Cell 9</td>
    </tr>
</table>