为什么我的javascript不能工作

Why isnt my javascript working?

本文关键字:工作 不能 javascript 我的 为什么      更新时间:2023-09-26

我所有的代码似乎工作除了我的javascript我做错了什么?谢谢,我只是初学者!

我试图使背景改变时,鼠标经过"标签"选项卡,但它不会这样做吗?发生了什么事?

HTML:

<html>
<head>
<script language="JavaScript" type="text/javascript">
// This changes color on mouseover, leaves existing color box.
$('.tab-item').mouseover(function() {
$(this).addClass("tab-mouseover");  
}).mouseout(function() {
$(this).removeClass("tab-mouseover");
});

// This changes color when clicked, removed old color box. 
$('.tab-item').click(function() {
$('.tab-item').removeClass("tab-selected");
$(this).addClass("tab-selected");
});-->
</script>
<link href="arg.css" rel="stylesheet" type="text/css" />
</head>
<body>

<div class="tab-item tab-selected" id="search-box">
Search
</div>
<div class="tab-item" id="tag-box">
Tags
</div>
</body> 
</html>
CSS:

.tab-item {
-moz-border-radius: 10px;
border-radius: 10px;
font: 14px helvetica;
color: #000;
height: 20px;
float: left;
margin: 5px 5px 5px 5px;
padding: 5px 5px 5px 5px;
position: relative;
width: 75px;
}
.tab-mouseover {
background: #bdbdbd;
}
.tab-selected {
background: #c0c0c0; 
}

谢谢!

詹姆斯

您正在使用jQuery,但尚未包含它。您还需要将jquery代码放入jquery ready事件:

<script type="text/javascript"  src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
// This changes color on mouseover, leaves existing color box.
$(function(){
    $('.tab-item').mouseover(function() {
        $(this).addClass("tab-mouseover");  
    }).mouseout(function() {
        $(this).removeClass("tab-mouseover");
    });

// This changes color when clicked, removed old color box. 
    $('.tab-item').click(function() {
        $('.tab-item').removeClass("tab-selected");
        $(this).addClass("tab-selected");
    });
});
-->
</script>

您还没有将库(我认为是jQuery)添加为源代码。像这样添加:

<script src='http://foo.com/bar/library.js'></script>

如果你确实在使用jQuery,你可以直接添加以下代码使其工作:

<script src='http://code.jquery.com/jquery-1.6.4.js'></script>

请注意,以上意味着您取决于jQuery网站的可用性,而不是您自己的。

根据James对此的评论,是的,你可以完全放弃jQuery,但我建议你自己学习JavaScript,而不是从网站上复制代码。如果要更改onmouseover字段的背景颜色,请使用如下代码:

<div onmouseover="this.style.backgroundColor='#bdbdbd';" onmouseout="this.style.backgroundColor='white';">Search</div>

<div onmouseover='this.className="tab-mouseover"' onmouseout='this.className=""'>Search</div>

或者不使用JavaScript,只使用简单的CSS:

<style>
.tab-mouseover:hover{
background: #bdbdbd;
}
</style>
<div class='tab-mouseover'>Search</div>

我不能回答后面的部分,因为我不明白在onclick元素中删除然后添加相同的类的用法

首先,您没有在代码中包含指向jQuery库的链接。因此,无论你把代码放在哪里,它都不能工作。

其次,由于您的代码位于文档headscript元素中,因此它将在呈现文档的body之前执行。你需要把它放在

$(document).ready(function() {
    /*
     * Your code here
     */
});

试试这个:

$('.tab-item').hover(
    function() {
        $(this).addClass('tab-mouseover');
    },
    function() {
        $(this).removeClass('tab-mouseover');
    }
);
$('.tab-item').click(function() {
    $('.tab-selected').removeClass('tab-selected');
    $(this).addClass('tab-selected');
});
http://jsfiddle.net/7dDTv/1/