鼠标悬停时仅悬停一行
请帮我解决问题:)
HTML :
<body>
<div>
<header></header>
<nav>
<table align="left" border="1">
<tr>
<td>
<div class="mainmenu">
aaa
</div></td>
</tr>
<tr class="b">
<td>
<table align="left" border="1" class="submenu">
<tr>
<td>
<p class="1">
aaaaa
</p></td>
</tr>
<tr>
<td>
<p class="1">
bbbbbbb
</p></td>
</tr>
<tr>
<td>
<p class="1">
ccccccccccccccccccccc
</p></td>
</tr>
<tr>
<td>
<p class="1">
ddddd
</p></td>
</tr>
</table></td>
</tr>
<tr>
<td>
<div class="menu">
bbbbbbb
</div></td>
</tr>
<tr>
<td>
<div class="menu">
ccccccccccccccccccccc
</div></td>
</tr>
<tr>
<td>
<div class="menu">
ddddd
</div></td>
</tr>
</table>
CSS : `
body {
background-color: orange;
color: white;
}
main{
color: blue;
width: 80%;
}
table.submenu{
background-color: #FF0033;
}
Jquery 脚本:
$(document).ready(function() {
$('tr.b').hide();
$('div.mainmenu').click(function() {
$('tr.b').toggle(400);
return false;
});
$('div.mainmenu, .menu').hover(function() {
$('div.mainmenu, .menu').css('color', 'pink');
}, function() {
$('div.mainmenu, .menu').css('color', 'white');
});
});
$(document).ready(function() {
$('tr.b').mouseover(function() {
$('p.1').css('color', 'blue');
});
$('tr.b').mouseout(function() {
$('p.1').css('color', 'gray');
});
});
please help me resolve the problem :)
HTML :
<body>
<div>
<header></header>
<nav>
<table align="left" border="1">
<tr>
<td>
<div class="mainmenu">
aaa
</div></td>
</tr>
<tr class="b">
<td>
<table align="left" border="1" class="submenu">
<tr>
<td>
<p class="1">
aaaaa
</p></td>
</tr>
<tr>
<td>
<p class="1">
bbbbbbb
</p></td>
</tr>
<tr>
<td>
<p class="1">
ccccccccccccccccccccc
</p></td>
</tr>
<tr>
<td>
<p class="1">
ddddd
</p></td>
</tr>
</table></td>
</tr>
<tr>
<td>
<div class="menu">
bbbbbbb
</div></td>
</tr>
<tr>
<td>
<div class="menu">
ccccccccccccccccccccc
</div></td>
</tr>
<tr>
<td>
<div class="menu">
ddddd
</div></td>
</tr>
</table>
CSS : `
body {
background-color: orange;
color: white;
}
main{
color: blue;
width: 80%;
}
table.submenu{
background-color: #FF0033;
}
Jquery script:
$(document).ready(function() {
$('tr.b').hide();
$('div.mainmenu').click(function() {
$('tr.b').toggle(400);
return false;
});
$('div.mainmenu, .menu').hover(function() {
$('div.mainmenu, .menu').css('color', 'pink');
}, function() {
$('div.mainmenu, .menu').css('color', 'white');
});
});
$(document).ready(function() {
$('tr.b').mouseover(function() {
$('p.1').css('color', 'blue');
});
$('tr.b').mouseout(function() {
$('p.1').css('color', 'gray');
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
工作演示
使用
$(this)
WORKING DEMO
use
$(this)
首先,我很好奇为什么您立即对 JavaScript 代码(在本例中使用 jQuery 库)感到困扰?您几乎可以通过很少 CSS 代码来实现这一点。
修改您的代码(进行一些更正):
标记:
CSS:
更实用的解决方案:
问题的解决方案是在
中使用伪类选择器 (
,因此每当您将鼠标指向该行时,它都会更改其样式。它比立即使用 jQuery 更简单和方便(这只会增加页面的文件大小,这在将来会影响网站的性能)。:hover
)进一步说明:
align="left"
已被弃用,避免使用它,改用 CSS (text-align: left
)。ltr
(从左到右),因此您实际上不需要在上声明它。 。
,以便用户了解
的全部内容。
摘要
来告诉屏幕阅读器其目的是什么(这是的长版本,但是更详细)嗯>。
、
和
。它们在 XHTML 中是必需的(如果您打算使用它的话),并且它们还有助于告诉阅读您代码的人这些
所属的位置。
,您想在另一个表中嵌套表来实现什么目的?在我看来,这会让你的用户界面在未来变得臃肿。
before anything else, I'm curious why you immediately bothered with JavaScript codes, in this case, using the jQuery library? You can pretty much achieve that with very few CSS codes.
Revising Your Code (With a few corrections):
The Markup:
The CSS:
A more practical solution:
The solution in your problem is to use the pseudo-class selector (
:hover
) in the<tr>
, so whenever you point your mouse on that row, it will change its style. It's more easy and convenient than immediately using jQuery (which will just increase your page's file size, which in the future, will affect the performance of your site).Further Notes:
align="left"
is already deprecated, avoid using it, use CSS instead (text-align: left
).ltr
(left-to-right), so you don't really need to declare that on the<table>
.<caption>
so users will know what the<table>
is all about.summary
to tell screen readers what's its purpose (it's the long version of<caption>
, but a more detailed one).<thead>
,<tbody>
, and<tfoot>
. They are required in XHTML (if you ever plan to use it), and they are also useful to tell people who read your code where those<tr>
belong to.<div>
s for no reason, and what are you trying to achieve on nesting tables inside another table? That will bloat your UI in the future, in my opinion.