具有悬停的元素然后单击会删除悬停效果,再次单击会再次添加悬停效果

发布于 2024-12-11 19:46:02 字数 804 浏览 0 评论 0原文

我正在尝试使用 JQuery 创建一个搜索词拼图框。基本上是一个每个单元格中都有字母表的表格,用户需要通过单击表格单元格来查找并标记网格中的单词。因此,我尝试按以下方式组合单击和悬停事件:

当鼠标悬停时,所有单元格都应具有悬停突出显示效果,除非已经单击了鼠标。如果单击它,那么它应该更改为不同的颜色来标记活动选择,以便删除悬停效果。再次单击所选单元格后,它应该恢复到原始状态,并添加悬停突出显示效果。进一步的点击只会重复上述切换。

怎么可能呢?我已经尝试使用 unbind()、bind() 选项执行以下操作,但它不起作用。谢谢,阿提拉

$("#puzzleTable td").each(function(){
$(this).hover(
   function(){
       $(this).css("background-color", "#FF6633");
   },
   function() {
       $(this).css("background-color", "#99CC00");
   }).toggle(
       function(){
       $(this).unbind('mouseenter mouseleave'),
       $(this).css("background-color", "#006699")
       },
       function(){      
       $(this).css("background-color", "#99CC00"),              
       $(this).bind('mouseenter mouseleave')
       }
   );
});

I am trying create a search word puzzle box with JQuery. Basically a table with an alphabet in each cell and the user needs to find and mark the words in the grid by clicking the table cells. So I am trying to combine the clicks and hover events the following way:

All cells should have a hover highlight effect when the mouse is over except when it was already clicked. If it was clicked then it should just change to a different color to mark active selection so the hover effect is removed. Upon clicking again the selected cell it should revert back to its original state with the hover highlight effect added. Further clicks would just repeat the above mentioned toggle.

How is it possible? I have tried the following with unbind(), bind() option but it didn't work. Thanks, Attila

$("#puzzleTable td").each(function(){
$(this).hover(
   function(){
       $(this).css("background-color", "#FF6633");
   },
   function() {
       $(this).css("background-color", "#99CC00");
   }).toggle(
       function(){
       $(this).unbind('mouseenter mouseleave'),
       $(this).css("background-color", "#006699")
       },
       function(){      
       $(this).css("background-color", "#99CC00"),              
       $(this).bind('mouseenter mouseleave')
       }
   );
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仅一夜美梦 2024-12-18 19:46:02

我将通过绑定两个事件来完成这一切:单击和悬停。其中每一项都有自己的逻辑,并且彼此独立运作。此外,由于您想要做的只是实现外观更改,因此您可以通过添加/删除 CSS 类来完成,而不是直接更新 CSS(即使用内联样式)。

这是一个小提琴: http://jsfiddle.net/VCf3E/

示例函数(类和颜色不是从您的示例代码):

$('table td')
    .hover(
   function(){
       $(this).addClass('hover');
   },
   function() {
       $(this).removeClass('hover');
   })
    .click(
    function() {
       $(this).toggleClass('active');
});

I would do it all by binding two events: the click and the hover. Each of these would have their own logic to work out and would operate independently of one-another. Further, since all you want to do is effect cosmetic changes, you could do it by adding/removing CSS classes rather than updating the CSS directly (ie. with inline styles).

Here's a fiddle: http://jsfiddle.net/VCf3E/

Sample function (classes and colours not taken from your sample code):

$('table td')
    .hover(
   function(){
       $(this).addClass('hover');
   },
   function() {
       $(this).removeClass('hover');
   })
    .click(
    function() {
       $(this).toggleClass('active');
});
一抹苦笑 2024-12-18 19:46:02

您的代码在重新绑定 mouseenter 和 mouseleave 事件时未指定事件处理程序。但是,如果您使用 CSS 类来应用样式,则根本不必取消绑定并重新绑定鼠标事件。只需定义“单击”状态规则,使其覆盖“悬停”状态规则即可。这非常简单,特别是如果您不需要支持 IE6(IE6 解决方案也包含在下面):

CSS 样式:

td {
    background-color: #99CC00;
}

td:hover {
    background-color: #FF6633;
}

td.clicked, td.clicked:hover {
    background-color: #006699;
}

Javascript:

$("#puzzleTable td").click(function () { $(this).toggleClass('clicked'); });

如果您需要支持 IE6,它会变得有点复杂(因为 IE6 只支持 :hover 在锚元素上):

CSS:

td {
    background-color: #99CC00;
}

td.hover {
    background-color: #FF6633;
}

td.clicked {
    background-color: #006699;
}

Javascript:

var cells = $('#puzzleTable td');
cells.bind('mouseenter mouseleave', function() { $(this).toggleClass('hover'); });
cells.click(function() { $(this).toggleClass('clicked'); });

Your code doesn't specify an event handler when it rebinds the mouseenter and mouseleave events. However, if you use CSS classes to apply your styles, you won't have to unbind and rebind the mouse events at all. Simply define your "clicked" state rules such that they override the "hover" state rules. This is pretty easy, especially if you don't need to support IE6 (IE6 solution included below as well):

CSS Styles:

td {
    background-color: #99CC00;
}

td:hover {
    background-color: #FF6633;
}

td.clicked, td.clicked:hover {
    background-color: #006699;
}

Javascript:

$("#puzzleTable td").click(function () { $(this).toggleClass('clicked'); });

If you need to support IE6, it gets a bit more complex (since IE6 only supports :hover on anchor elements):

CSS:

td {
    background-color: #99CC00;
}

td.hover {
    background-color: #FF6633;
}

td.clicked {
    background-color: #006699;
}

Javascript:

var cells = $('#puzzleTable td');
cells.bind('mouseenter mouseleave', function() { $(this).toggleClass('hover'); });
cells.click(function() { $(this).toggleClass('clicked'); });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文