windows.location无法处理click事件

时间:2012-01-30 19:07:45

标签: javascript jquery

我遇到点击事件无法正常触发的问题。我所引用的click事件是附加到#sales的页面底部的那个。我使用microsoft.com只是为了确保它不是我试图导航到的页面。任何帮助将不胜感激,代码如下。

// JavaScript Document

$(document).ready(function() {

$(".intro-no-js").css("visibility","hidden");
$(".intro-javascript").css("visibility","visible");

//defines how max should be animated
jQuery.fn.maxanimate = function () { 
this.css("position","absolute"); 

    var t = (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop(); 

    var l = (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft(); 

    $(this).animate({
        top: t,
        left: l-79.5
    }, 500);

    return this; 
} 


//defines how xim should be animated
jQuery.fn.ximanimate = function () { 
this.css("position","absolute"); 

    var t = (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop(); 

    var r = (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft(); 

    $(this).animate({
        top: t,
        left: r+78.5
    }, 500);

    return this; 
} 

//defines how top arrows should be animated
jQuery.fn.arrowsanimate = function () { 
this.css("position","absolute"); 

    var t = (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop(); 
    var l = (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft(); 

    $(this).animate({
        top: t-25,
        left: l
    }, 500);

    return this; 
} 

//defines how bottom section should be animated
jQuery.fn.animatebottom = function () { 
this.css("position","absolute"); 

    var b = (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop(); 
    var l = (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft(); 

    $(this).animate({
        bottom: b-22,
        left: l
    }, 500);

    return this; 
}

//max starting co-ordinates
var maxl = $(window).width() - 157; 
var maxt =  ($(window).height() - $("#intro-max").outerHeight()) / 2; 

//xim starting co-ordinates
var ximr = $(window).width() - 159; 
var ximt = ($(window).height() - $("#intro-xim").outerHeight()) / 2; 

//arrows starting co-ordinates
var al = (($(window).width() -  $("#intro-xim").outerWidth()) / 2) + $(window).scrollLeft()+ 35; 
var at = 0; 

//bottom of logo starting co-ordinates
var bl = (($(window).width() -  $("#intro-xim").outerWidth()) / 2) + $(window).scrollLeft()-50;
var bt = 0;

//set initial co-ordinates for all divs
$("#intro-arrows").css({position: "absolute",top: at,left: al});    
$("#intro-max").css({position: "absolute",top: maxt,right: maxl});
$("#intro-xim").css({position: "absolute",top: ximt,left: ximr});
$(".intro-bottom").css({position: "absolute", bottom: bt, left: bl});

//lets bring it all to life!
$("#intro-max").maxanimate();
$("#intro-xim").ximanimate();
$("#intro-arrows").arrowsanimate();
$(".intro-bottom").animatebottom();



$("#sales").click(function() {
    window.location = "http://www.microsoft.com"; 
});

});

我的标记如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Maxxim - Welcome</title>
</head>
<script type="text/javascript" language="javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" language="javascript" src="js/intro.js"></script>
<link rel="stylesheet" type="text/css" href="css/intro.css">
<body>
<div class="intro-no-js">
<img src="imgs/intro/Intro-logo.gif" alt="Maxxim Logo">
<a href="sales-and-marketing.html" title="Sales & Marketing"><img src="imgs/intro/Intro-sales-butt.gif" alt="Sales & Marketing" id="sales"></a>
<a href="design.html" title="Design"><img src="imgs/intro/Intro-design-butt.gif" alt="Design" id="design"></a>
</div>

<div class="intro-javascript">
<div id="intro-arrows"></div>
<div id="intro-max"></div>
<div id="intro-xim"></div>

<div class="intro-bottom">
    <div id="pants"></div>
    <div id="sales"></div>
    <div id="design"></div>
</div>
</div>

</body>
</html>

这是CSS:

@charset "utf-8";
/* CSS Document */
body
{
background-color: #000;
margin: 0 0;
}

/* Styling for if javascript is disabled */
.intro-no-js
{
width: 317px;
margin: 0 auto;
position: relative; 
top: 275px;
}

.intro-no-js sales
{
float: left;
}

.intro-no-js design
{
float: left;
}

.intro-no-js img
{
border: none;
}

/* Styling for animation if javascript is enabled */
.intro-javascript
{
visibility: visible;
}

#intro-arrows
{
background-image:url(../imgs/intro/Intro-logo_top.png);
width: 53px;
height: 13px;
}

#intro-max
{
background-image:url(../imgs/intro/Intro-logo_left.png);
width: 159px;
height: 72px;
}

#intro-xim
{
background-image:url(../imgs/intro/Intro-logo_right.png);
width: 157px;
height: 72px;
}


.intro-bottom
{
width: 317px;
}
#pants
{
margin: 0 auto;
background-image: url(../imgs/intro/Intro-logo_bottom.png);
width: 80px;
height: 37px;

}

#sales
{
background-image: url(../imgs/intro/Intro-sales-butt.gif);
width: 218px;
height: 8px;
float: left;
}

#sales:hover
{
background-image: url(../imgs/intro/Intro-sales-butt-o.gif);
}

#design
{
background-image:url(../imgs/intro/Intro-design-butt.gif);
width: 99px;
height: 8px;
float: left;
}

#design:hover
{
background-image:url(../imgs/intro/Intro-design-butt-o.gif);
}

1 个答案:

答案 0 :(得分:6)

试试这个。

$("#sales").click(function() {
    window.location.href = "http://www.microsoft.com"; 
    return false;//Just in case if it is a link prevent default behavior
});

查看您提供的链接,我发现了问题。您对页面上的多个元素具有相同的ID。所以当你尝试按id获取元素时,如果页面上有多个元素,jQuery将返回带有该id的第一个元素。

您在具有相同身份<img src="imgs/intro/Intro-sales-butt.gif" alt="Sales & Marketing" id="sales">的网页上也有sales。更改它应该正常工作的ID。