[英]How to get a first cell from a row that user hovers over?
How to get a first cell from a row that user hovers over and then clicks certain key combination?(i am thinking about using jQuery) I have a table that is similar to that one. 如何从用户悬停的行中获取第一个单元格,然后单击某些键组合?(我正在考虑使用jQuery)我有一个与此表相似的表。 When user places mouse over any of tr's and press ctrl+c he gets the ID copied to his clipboard.
当用户将鼠标放在任意一个tr上并按ctrl + c时,会将ID复制到剪贴板。 Thank you!
谢谢! UPD.Code that worked bellow.
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;
}
}
});
});
Welcome to Stackoverflow. 欢迎使用Stackoverflow。 First of all, please read the guidelines on how to ask a question.
首先,请阅读有关如何提问的指南。
Key of this guideline is: 该指南的关键是:
stackoverflow members won't do your work, but they'll help you, if you are stuck with a problem.
stackoverflow成员不会做您的工作,但是如果您遇到问题,他们会为您提供帮助。 To help you, we would need a 'What have I done so far' passage inside your question.
为了帮助您,我们需要在您的问题中注明“到目前为止我做了什么”。
Do you know jquery? 你知道jQuery吗? and how the selectors work?
以及选择器如何工作? here the steps for your what have I done passage:
这是您为我所做的事情的步骤:
The table row selector $("table tr")
(but I'd suggest working with classes and id's) 表行选择器
$("table tr")
(但我建议使用类和ID)
The selector to get the first cell: 选择器获取第一个单元格:
$("> td:first", this)
The jquery function to get the content of the first td . jQuery函数获取第一个td的内容 。
The clipboard problem, already discussed here . 剪贴板问题,已在此处讨论。
You can get the cell value in different ways. 您可以通过不同的方式获取单元格值。 Below is the simple script that does that.
下面是执行此操作的简单脚本。
JSFIDDLE: http://jsfiddle.net/kiranvarthi/cr399ww2/ (click to see value alert) JSFIDDLE: http : //jsfiddle.net/kiranvarthi/cr399ww2/ (单击以查看值警报)
$( "table tr" ).click(function() {
alert($(this).children('td').first().html());
});
Once you get the ID, you can just add to clip board. 一旦获得ID,就可以添加到剪贴板。 Below link help to do that.
下面的链接帮助您做到这一点。 How do I copy to the clipboard in JavaScript?
如何使用JavaScript复制到剪贴板?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.