简体   繁体   English

jQuery .hover无法在Safari或Chrome中运行

[英]jQuery .hover not working in Safari or Chrome

I am currently trying to make some jQuery hover effects render correctly in all browsers. 我目前正在尝试在所有浏览器中正确渲染一些jQuery悬停效果。 For the moment, firefox, IE, opera all do what they are supposed to. 目前,firefox,IE,Opera都做了他们应该做的事情。 However, Safari and Chrome do not. 但是,Safari和Chrome没有。

The code looks like this: 代码如下所示:

<div id="button1">
    <div id="work_title" class="title_james">
    WORDS
</div>
</div>
<div id="button2">
    <div id="work_title" class="title_mike">
    MORE WORDS
</div>
</div>

and the script effecting it looks like this 影响它的脚本看起来像这样

<script>
$(function() {
$("#button2").hover(
function() {
    $("#james").css('z-index', '100')
    $(".title_mike").css('width', '590px')
}, function() {
    $("#james").css('z-index', '')
    $(".title_mike").css('width', '')
});
});​
$(function() {
$("#button1").hover(
function() {
    $(".title_james").css('width', '785px')
}, function() {
    $(".title_james").css('width', '')
});
});​
</script>

what I am trying to get it to do is change the css styles two elements on hover over two large areas of text.. 我试图让它做的是改变css样式两个元素悬停在两个大的文本区域..

I have tried the mouseenter .addClass and mouseleave .removeClass thing and that didn't work at all.. so when I got this to work in firefox I was all happy... then I did cross browser checking and I got sad again.. 我已经尝试了mouseenter .addClass和mouseleave .removeClass的东西,而且根本不起作用..所以当我在firefox中工作时我很高兴...然后我做了跨浏览器检查,我又伤心了。 。

You can see it live in action at: http://roboticmonsters.com/who 您可以在http://roboticmonsters.com/who上看到它的实际效果

Using the dev tools in Chrome it says there is an invalid token at the end of each of the javascript functions. 使用Chrome中的开发工具,它表示每个javascript函数末尾都有一个无效的令牌。 The IE dev tools shows an invalid token too, but it seems to ignore this and render correctly. IE开发工具也显示了无效的令牌,但似乎忽略了这一点并正确呈现。 Check your source and remove the token, if you can. 如果可以,请检查您的来源并删除令牌。

IE: IE:

在此输入图像描述

Chrome: 铬:

在此输入图像描述

$.css takes an object: $ .css接受一个对象:

$("#james").css({'z-index': '100'});

Note the curly braces and colon (not comma). 注意花括号和冒号(不是逗号)。

This is so you can specify several css rules in one: 这样你就可以在一个中指定几个css规则:

$("#james").css({'z-index': '100', 'height': '100px'});

If you are getting the value of a css rule, just pass in the name as a string: 如果您获得了css规则的值,只需将名称作为字符串传递:

$("#james").css('z-index'); // returns 100

if just use the code below it works fine: 如果只是使用下面的代码,它工作正常:

$("#button2").hover(
function() {
    $("#james").css('z-index', '100')
    $(".title_mike").css('width', '590px')
}, function() {
    $("#james").css('z-index', '')
    $(".title_mike").css('width', '')
});

Otherwise Chrome reports: Unexpected token ILLEGAL . 否则,Chrome会报告: Unexpected token ILLEGAL To see this yourself, right-click on the page and choose inspect element . 要自己查看,请右键单击页面并选择inspect element Click the small red x in the bottom right. 单击右下角的小红色x

Update : actually your code works fine if you remove the illegal character as shown in @anothershubery's answer 更新 :实际上,如果您删除@ anothershubery的答案中显示的非法字符,您的代码将正常工作

It's possibly because you are trying to bind to those events before the DOM has loaded. 这可能是因为您在DOM加载之前尝试绑定到这些事件。

I didn't have much time to give you an answer as to why it was broken, but the following works for me in chrome. 我没有太多时间给你一个答案,为什么它被打破了,但以下对我来说是有用的。

    $(document).ready(function() {
    $("#button2").hover(function() {
                $("#james").css('z-index', '100');
                $(".title_mike").css('width', '590px');
                },
                function() {
                $("#james").css('z-index', '');
                $(".title_mike").css('width', '');      
                }
            );
    $("#button1").hover(function() {
                $(".title_james").css('width', '785px');
                },
                function() {
                $(".title_james").css('width', '');     
                }
            );
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM