简体   繁体   English

如何从用户悬停的行中获取第一个单元格?

[英]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. 为了帮助您,我们需要在您的问题中注明“到目前为止我做了什么”。

Now to your problem. 现在解决您的问题。

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 jquery hover function jQuery悬停功能

  • 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.

相关问题 用户将鼠标悬停在文本上方时如何删除文本 - how to strike out text when a user hovers over it 隐藏说明直到用户将鼠标悬停在图片上? - Description hidden until user hovers over the image? 当用户单击选项时如何隐藏子菜单,但当用户将鼠标悬停在菜单上时仍会显示 - How to hide submenu when user clicks option, but still display when user hovers over menu 如何从html表中选择行并获取第一个单元格的值并将其放在PHP Session上 - How select row from html table and get the value of the first cell and put it on PHP Session 用户将鼠标悬停在src链接地址时,将其从png更改为gif - Change a src link address from png to gif when a user hovers over it 如何从单元格中获取前 3 位数字 - How to get the first 3 digits from a cell 用户将鼠标悬停在某些文本上时如何打开小窗口? - How can I open a small window when the user hovers over some text? 当用户将鼠标悬停在HTML文档中的静态图片上时如何显示Google地图 - How to show google map when user hovers over static image in html document 当用户将鼠标悬停在元素的框阴影上方时,如何显示工具提示? - How can I show a tooltip when a user hovers over the box shadow of an element? 当用户将鼠标悬停在某个地址上时,如何显示弹出式Google Map? - How can I have a popup Google Map appear when the user hovers over an address?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM