如何从用户悬停的行中获取第一个单元格,然后单击某个组合键?(我正在考虑使用 jQuery) 我有一张与那张相似的 table 。当用户将鼠标放在任何 tr 上并按 ctrl+c 时,他会将 ID 复制到他的剪贴板。 谢谢你! UPD.代码在下面工作。
<table>
<tr>
<td>ID</td>
<td>Text1</td>
<td>Text2</td>
<td>Text3</td>
</tr>
<tr>
<td id="my_id">1</td>
<td>Example1</td>
<td>Example1</td>
<td>Example1</td>
</tr>
<tr>
<td id="my_id">2</td>
<td>Example2</td>
<td>Example2</td>
<td>Example2</td>
</tr>
<tr>
<td id="my_id">3</td>
<td>Example3</td>
<td>Example3</td>
<td>Example3</td>
</tr>
</table>
$(document).ready(function()
{
var ctrlDown = false;
var ctrlKey = 17, cKey = 67;
var id = "";
$(document).keydown(function(e) {
if (e.keyCode == ctrlKey) ctrlDown = true;
});
$("tr").mouseover(function(){
id = $(this).find("#my_id").html();
});
$("tr").mouseout(function(){
id = "";
});
$(document).keydown(function(e)
{
if (ctrlDown && e.keyCode == cKey) {
if (id != "") {
window.prompt("Copy to clipboard: Ctrl+C, Enter", id);
ctrlDown = false;
}
}
});
});
最佳答案
欢迎使用 Stackoverflow。首先,请阅读有关如何提问的指南。
该指南的关键是:
stackoverflow members won't do your work, but they'll help you, if you are stuck with a problem. To help you, we would need a 'What have I done so far' passage inside your question.
现在解决你的问题。
你知道jquery吗?选择器是如何工作的?这是您的“我做了什么”段落的步骤:
表格行选择器
$("table tr")
(但我建议使用类和 ID)获取第一个单元格的选择器:
$("> td:first", this)
获取 content of the first td 的 jquery 函数.
剪贴板问题,已经讨论过here .
关于javascript - 如何从用户悬停的行中获取第一个单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26710385/