Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在html表中获取悬停元素_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 如何在html表中获取悬停元素

Javascript 如何在html表中获取悬停元素,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我想在html表格中获得悬停元素 当我获得悬停id时,我可以更改它的类。我发现jQuery(“:hover”)在这种情况下很有用 有什么办法可以实施吗 谢谢 jQuery(“:hover”)? 1. 2. 3. 4. 5. 6. 7. 8. 9 将悬停事件添加到表tds $('table td')。悬停(函数(){ const id=$(this.attr('id'); console.log(id); }); 1. 2. 3. 4. 5. 6. 7. 8. 9 您的意思可能是使用以下方

我想在html表格中获得
悬停元素

当我获得
悬停id
时,我可以更改它的类。我发现
jQuery(“:hover”)
在这种情况下很有用

有什么办法可以实施吗

谢谢

jQuery(“:hover”)?

1.
2.
3.
4.
5.
6.
7.
8.
9

悬停
事件添加到表
tds

$('table td')。悬停(函数(){
const id=$(this.attr('id');
console.log(id);
});

1.
2.
3.
4.
5.
6.
7.
8.
9

您的意思可能是使用以下方法添加悬停事件:

$('table tr td')。悬停(函数(){
console.clear();//清除以前的日志
console.log(this.id);
$(this.css({color:'#fff',backgroundColor:'blue'});
});
td{
光标:指针;
}

1.
2.
3.
4.
5.
6.
7.
8.
9

mouseover
上捕捉id,并在
mouseout

css仅用于演示,您可以忽略它

$('table td')。on('mouseover mouseout',函数(e){
设hoverId=$('table td:hover').attr('id');
$('#HoveredId').html((hoverId?'hovered id='+hoverId:'');
});
#HoveredId{
字体大小:14px;
填充:15px;
}
表td{
填充:10px;
保证金:5px;
背景:#000;
颜色:#fff;
}

1.
2.
3.
4.
5.
6.
7.
8.
9

很难理解你在问什么,但是你可以将
hover
监听器添加到像这样的元素中


如果您只想在元素悬停时更改样式,那么绝对不应该使用Javascript。简单的CSS就可以了,而且速度要快几个数量级

td[id]:悬停{
背景色:#f0;
}

1.
2.
3.
4.
5.
6.
7.
8.
9
我强烈建议您不要为此使用Javascript,如果必须,请使用普通的DOM API,而不是jQuery。