简体   繁体   English

jQuery适用于Firefox,Safari,但不适用于Chrome

[英]jQuery working in Firefox, Safari but not in Chrome

I have written some code with jquery works in firefox, safari and ie9. 我已经在Firefox,Safari和ie9中用jquery编写了一些代码。 But chrome does not like it. 但是Chrome不喜欢它。 No obvious msgs in chrome console coming up. chrome控制台中没有明显的消息。 I am hitting a wall hopefully someone can shed some light. 希望我能碰到一堵墙,希望有人可以照亮。 Script just show/hides some tooltips. 脚本仅显示/隐藏一些工具提示。 Any ideas? 有任何想法吗? fiddle here, changed code still no change to behaviour. 在这里摆弄,改变的代码仍然没有改变行为。

http://jsfiddle.net/qAfwJ/ http://jsfiddle.net/qAfwJ/

$(document).ready(function(){
    //custom toolTip Written by CASherwood but not working in ie9/chrome
        var ShowId;
        var id;
        var contentholder = $(".contentBox");
        var toolTip = $(".info");
        var idHashString = '#info';
        var idString = 'id';

            function showToolTip(name, id){
                  id = name + id;
                  $(id).fadeIn(1000);
            }

            function hideToolTip(name, id){
                  id = name + id;
                  $(id).fadeOut(1000);
            }

        $(toolTip).mouseover(function(){
                ShowId = $(this).attr(idString);
                showToolTip(idHashString, ShowId);
        });

        $(contentholder).mouseleave(function(){
            ShowId = $(this).find('.info').attr(idString);
            hideToolTip(idHashString, ShowId);
        });


});

There are a few things here, You are setting a variable var toolTip = $(".info"); 这里有几件事,您正在设置一个变量var toolTip = $(".info");

And then using this same variable to add a function to it. 然后使用相同的变量向其添加函数。 What you are doing here is actually 你在这里实际上是在做

$($(".info")).mouseover(

Instead of 代替

var toolTip = $(".info");
toolTip.mouseover(

Also you might consider using 您也可以考虑使用

jquery.hover(handlerIn(eventObject) , handlerOut(eventObject)  );

http://api.jquery.com/hover/ http://api.jquery.com/hover/

Ok one thing I'm noticing here is that you are wrapping some elements twice with the jQuery selector. 好的,我在这里注意到的一件事是,您使用jQuery选择器将某些元素包装了两次。

var contentholder = $(".contentBox");
$(contentholder).mouseleave(function(){
  ...
});

Basically what evaluates to is this - 基本上评估是这个-

$($(".contentBox")) 

That doesn't look too good and I'm not too sure if it would work as expected. 这看起来不太好,我不太确定它是否可以按预期工作。 Even if it does, the issues of cross browser compatibility might come into play and I believe this is what you are experiencing. 即使是这样,跨浏览器兼容性的问题也可能会起作用,我相信这就是您正在遇到的问题。 If you have already captured the element and are not just storing the selectors as strings, then there is no need to wrap the element again with the $ syntax. 如果您已经捕获了元素,而不仅仅是将选择器存储为字符串,则无需使用$语法再次包装该元素。

var contentholder = $(".contentBox");
contentholder.mouseleave(function(){
  ...
});

When you are constructing selectors from strings and variables, you should do so in a similar way to this - 当您从字符串和变量构造选择器时,应采用与此类似的方式-

var elementId = 'the_elements_id';
$('#'+elementId).on('click',handler);

I'd start by changing 我将从改变开始

        $(toolTip).mouseover(function(){
                ShowId = $(this).attr(idString);
                showToolTip(idHashString, ShowId);
        });

        $(contentholder).mouseleave(function(){
            ShowId = $(this).find('.info').attr(idString);
            hideToolTip(idHashString, ShowId);
        });

to

        toolTip.mouseover(function(){
                ShowId = $(this).attr(idString);
                showToolTip(idHashString, ShowId);
        });

        contentholder.mouseleave(function(){
            ShowId = $(this).find('.info').attr(idString);
            hideToolTip(idHashString, ShowId);
        });

since your toolTip and contentholder variables are already jquery objects. 因为您的toolTip和contentholder变量已经是jquery对象。

I'm not sure and haven't tested it, but what if you try to move the two functions ( showToolTip() and hideToolTip() ) before or after the $(function(){}); 我不确定并且还没有测试它,但是如果您尝试在$(function(){});之前或之后移动两个函数( showToolTip()hideToolTip() ),该hideToolTip() $(function(){}); The might get seen as inner functions of some sort instead of global functions and that might be a thing. 可能将其视为某种内部函数,而不是全局函数,这可能是一回事。

暂无
暂无

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

相关问题 jQuery适用于Chrome和Safari,但不能适用于Firefox或IE? - jQuery working in Chrome and Safari, but not Firefox or IE? jQuery on()在Chrome,Safari中不起作用。 在Firefox中工作 - jQuery on() not working in Chrome, Safari. Works in Firefox jQuery放大无法在Chrome和Safari中使用,但可以在Firefox中使用 - jQuery magnify not working in Chrome and Safari but Firefox 可以在Chrome中正常工作的JavaScript jQuery代码,而不能在Firefox,Safari,IE中运行 - Simply javascript jQuery code working in Chrome, not Firefox, Safari, IE Javascript / jQuery无法在Firefox,Safari和IE中运行。 精通Opera和Chrome - Javascript/jQuery not working in Firefox, Safari and IE. Fine in Opera and Chrome Javascript / JQuery突然在Chrome和Firefox中停止工作,但仍在Safari中工作 - Javascript/JQuery suddenly stops working in Chrome and Firefox, but still works in Safari Chrome和Safari中的jQuery动画错误,在Firefox中按预期工作 - JQuery animation Error in Chrome and Safari, in Firefox it's working as expected jQuery ajax函数在Safari中不起作用(Firefox,Chrome,IE可以) - jQuery ajax function not working in Safari (Firefox, Chrome, IE okay) jQuery .css()在IE 6,7,8和Firefox中不起作用,但在Chrome,Safari,Opera中起作用 - jQuery .css() not working in IE 6,7,8 and Firefox, but works in Chrome, Safari, Opera 使用Firefox而不使用Chrome,Safari的代码 - Code Working with Firefox but not with Chrome, Safari
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM