如果在第二个 td
上点击了前一个 td
的溢出文本,处理程序将返回第一个 td
作为文本属于第一个 td
。相反,我需要选择第二个 td
而不管它有什么溢出文本。
提前致谢。
最佳答案
我认为与其一直将属性设置为 true,不如在模糊时将其设置为 false,并且需要将 css 属性 position:relative;
设置为 td
:
$(function() {
$('td').on({
click: function() {
$(this).attr('contenteditable', 'true').focus();
},
blur: function() {
$(this).attr('contenteditable', 'false');
}
});
});
table {
border-collapse: collapse;
table-layout: fixed;
width: 500px;
}
td {
border: 2px solid black;
-webkit-user-select: none;
width: 25%;
height: 20px;
white-space: nowrap;
overflow: visible;
text-overflow: ellipsis;
padding: 0px;
position: relative; /*<-----add this position in the css*/
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<table>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</div>
关于javascript - 如果它有以前的 td 溢出文本,我如何通过单击获得 td,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32988044/