Addclass 不是 jQuery 中的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16865435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Addclass is not a function in jQuery
提问by Chris
I have a mouseover function that should add a class to my viewport element, but i get an error in firebug when I mouseover: TypeError: jQuery(...).addclass is not a function.
我有一个鼠标悬停函数,它应该向我的视口元素添加一个类,但是当我悬停鼠标时,我在萤火虫中得到一个错误:TypeError: jQuery(...).addclass is not a function。
the HTML is:
HTML是:
<!DOCTYPE html>
<html>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>" />
<title><?php wp_title('|','true','right'); ?><?php bloginfo('name'); ?></title>
<meta name="viewport" content="width=device-width" />
<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( 'stylesheet_url' ); ?>" />
<?php wp_head(); ?>
</head>
<body <?php body_class($class); ?>>
<header>
<div class="main-logo">
<div id="site-title">
</div><!--.site-title-->
</div><!--main-logo-->
<div class="header-right">
</div><!--header-right-->
</header>
<nav class="main">
<?php wp_nav_menu( array( 'theme_location' => 'main-menu' ) ); ?>
</nav>
<div class="viewport">
<script type="text/javascript"><!--//--><![CDATA[//><!--
jQuery(document).ready(function(jQuery){
jQuery('nav .home a').mouseover(function()
{
jQuery('.viewport').addclass('.viewporthome');
});
});
//--><!]]></script>
</div>
</div>
The related styles are:
相关样式如下:
.viewport
{
height: 400px;
width: 400px;
position: relative;
top: -90px;
margin: 0 auto;
}
.viewporthome
{
background-image: url('images/Screen2.png');
background-repeat: no-repeat;
background-position: center;
background-attachment: relative;
}
The JS file is:
JS文件是:
var hoverhome = 'url("images/Screen2.png")';
var empty = '';
var success = 'SUCCESS!';
//home
jQuery('nav .home a').hover(function()
{
jQuery('.viewport').css('background-image', hoverhome);
});
jQuery('nav .home a').mouseout(function()
{
jQuery('.viewport').css('background-image', empty);
});
回答by Karl-André Gagnon
Try this :
尝试这个 :
jQuery('.viewport').addClass('viewporthome');
addClass
need a capital "C".
addClass
需要一个大写的“C”。
Also, when adding class, you dont need to put the "." in the string or your class will be ..viewporthome
.
另外,在添加类时,您不需要放置“。” 在字符串中,否则您的班级将是..viewporthome
.
回答by Amir Mehdi Delta
Adding a class:
添加一个类:
$(".something").click(function(){
$(this).addClass("style");
});
Removing a class:
删除一个类:
$(".something").click(function(){
$(this).removeClass("style");
});
回答by A. Wolff
Remove extra dot and use addClass()
not addclass()
删除多余的点并使用addClass()
not addclass()
jQuery('.viewport').addClass('viewporthome');
回答by DRock Miller
Check your capitalization. The addClass method has a capital 'C'.
检查您的大小写。addClass 方法有一个大写的“C”。
If the image still isn't showing up it is probably an issue with the relative path to your image. If the images folder isn't in the same directory as your Javascript file you may have to modify the path. I can't tell exactly what you would need to do without knowing your directory structure.
如果图像仍未显示,则可能是图像的相对路径存在问题。如果图像文件夹与您的 Javascript 文件不在同一目录中,您可能需要修改路径。在不知道您的目录结构的情况下,我无法确切地说出您需要做什么。