JavaScript 悬停光标指针

14

试图使用 jQuery 在悬停时设置光标指针:

$('#example td').hover(function() {
$(this).css('cursor','pointer');
});

我不想把它放在CSS中,我想放在Jquery里。

我需要做什么来解决这个问题?

http://jsfiddle.net/Egttj/


1
你为什么不想使用CSS? - Blender
2
jQuery不是CSS的替代品。 - Blender
5
我该如何用玻璃瓶子钉钉子呢? - SLaks
1
@DJ Howarth:我不知道你在运行代码时犯了什么错误。我已经在jsFiddle上完美地执行了它。也许我猜你可能在执行时忘记打开jsFiddle中的jQuery了。 - Arunkumar Srisailapathi
4
“@Blender [它真的很棒,可以完成所有事情] (http://www.doxdesk.com/img/updates/20091116-so-large.gif)” - JJJ
显示剩余2条评论
3个回答

28
"It seems to work here after I cleared up your jQuery. You forgot to close the first click function, and the searchform and button aren't declared in your HTML (I changed it to #example tr).
Basically, your jQuery code was not well-written. There was also a line with 5.} that didn't belong there.
I also changed "your framework" to jQuery instead of the default selection, mooTools.
Here is the complete jQuery code after removing some unnecessary parts:"
$(document).ready(function() {

    $('#example tr').click(function() {
        var href = $(this).find("a").attr("href");
        if(href) {
            window.location = href;
        }
    });

    $('#example tr').hover(function() {
        $(this).css('cursor','pointer');
    });

});

Fiddle


9
由于某些原因我无法对Tobias Nilsons的回答进行评论,所以我会将其作为答案呈现。
$('#example td').css('cursor', 'pointer');`

没有理由要指定悬停时应用的样式。光标只有在悬停时才会出现。

示例:http://jsfiddle.net/Egttj/15/

我假设你只是对在样式表中简单指定相应规则感到有些困难。但是,正如大家所指出的那样,这确实是最好和最简单的方法。


1
比较第一种方法。
$('#example tr').hover(function() {
    $(this).css('cursor','pointer');
});

使用第二种方法
$('#example td').css('cursor', 'pointer');`

我建议坚持使用第一种方法,因为它可以扩展到任何我们想要的jQuery。
$('#example tr').hover(function() {

    $(this).css('cursor','pointer');

    $(this).click(function() {
       var href = $(this).find("a").attr("href");
       if(href) {
          window.location = href;
       }
    });

});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接