简体   繁体   English

为什么这个jQuery代码可以在Firefox中运行,但不适用于Chrome或Safari?

[英]Why does this jQuery code work in Firefox but not Chrome or Safari?

I'm using jQuery to highlight (by changing bg color) some table cells. 我正在使用jQuery突出显示(通过更改bg颜色)一些表格单元格。 I want the button (a simple anchor) to be given the class name 'selected' when clicked and I want certain table cells below the buttons to highlight. 我希望按钮(一个简单的锚点)在单击时被赋予类名“选中”,我希望按钮下方的某些表格单元格突出显示。 When you click the same button again, it deselects and removes the highlights from the table cells. 再次单击同一按钮时,它将取消选择并从表格单元格中删除高光。 When you click a different button, it removes the other highlights and switches to the new appropriate ones. 单击其他按钮时,将删除其他突出显示并切换到新的相应突出显示。

This all works perfectly in Firefox. 这一切都在Firefox中完美运行。 When I try it in a webkit browser, it does not. 当我在webkit浏览器中尝试它时,它没有。 I can't figure out why and it's driving me crazy! 我无法弄清楚为什么,它让我发疯! The jQuery code is below. jQuery代码如下。 You can see the page at: 您可以在以下位置查看该页面:

http://byegurl.com/scores.html http://byegurl.com/scores.html

$(function(){
$(".press").click(function() {
    id = $(this).attr("id");
    name = $("." + id);
    if ($(this).hasClass('selected')) 
         {
            $(this).removeClass('selected');
            $(name).removeClass('highlight');
         } else {
            $('.press').removeClass('selected');
            $("td:not(name)").removeClass('highlight');         
            $(this).addClass('selected');
            $(name).addClass('highlight');
         }
    return false; 
});
});         

I appreciate your help! 我感谢您的帮助!

Change: 更改:

id = $(this).attr("id");
name = $("." + id);

to: 至:

var id = $(this).attr("id");
var name = $("." + id);

That is, declare the variables with var so they have local scope. 也就是说,用var声明变量,使它们具有局部范围。 Without var , the variables had global scope and were conflicting with something. 如果没有var ,变量具有全局范围并且与某些内容相冲突。

It now works in Chrome/Safari: http://jsbin.com/efilok/ 它现在可以在Chrome / Safari中使用: http//jsbin.com/efilok/

A few things that may solve the problem: 可以解决问题的一些事情:

name is already a jQuery object. name已经是一个jQuery对象。 Change this line to: 将此行更改为:

name.removeClass('highlight');

and

name.addClass('highlight');

Also, instead of return false; 而且,而不是return false; , I recommend event.preventDefault() , like this: ,我推荐event.preventDefault() ,如下所示:

$('.press').click(function(event) {
    // ...
    event.preventDefault();
});

Apparently name is being used by chrome for some other purpose. 显然,chrome正在使用name来实现其他目的。 If you were to have your name variable not a global variable, ie. 如果你的name变量不是全局变量,即。 var name instead of just name then it would probably work. var name而不仅仅是name然后它可能会工作。 Though I would just use a different variable name. 虽然我只想使用不同的变量名称。

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

相关问题 为什么此jQuery代码在Chrome和Firefox中能正常工作,而在IE9上却不能工作? - Why does this jQuery code work fine in Chrome and Firefox, but not IE9? 为什么此代码在Safari中有效但在Chrome中不起作用? Arrrgh - Why does this code work in Safari but not Chrome? Arrrgh 为什么我的Javascript / jQuery可以在Chrome和Safari中运行,但不能在Firefox,IE9或Opera中运行? - Why does my Javascript/jQuery work in Chrome and Safari but not firefox, IE9, or Opera? 为什么此代码适用于Chrome,但不适用于Firefox - Why does this code work for chrome but not for firefox 为什么我的“以前”过滤器只能在Chrome中使用,而不能在Safari或Firefox中使用? - Why does my 'time ago' filter work in chrome and not safari or firefox? 为什么这在FireFox上有效但在Safari上无效? - Why does this work on FireFox but not on Safari? 为什么jquery可以在Chrome中工作,但不能在Safari中工作? - Why jquery work in Chrome but not on Safari? 为什么这些复选框在 Safari 中有效,但在 Chrome 或 Firefox 中无效? - Why do these checkboxes work in Safari but not Chrome or Firefox? 为什么这在Chrome中有效,但在Firefox中不起作用? - Why does this work in Chrome but not firefox? 为什么这个jQuery图像交换在Chrome和Firefox中的第一次加载时不起作用 - why does this jquery image swap not work in chrome and on first load in firefox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM