2013-03-04 35 views
2

这里是我的代码:jquery显示和隐藏不工作?缺少的东西,我不能弄明白

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
<script type="text/javascript">  
$(".social").hover(function() { 
     $("h1", this).hide(); 
     $(".networks", this).fadeIn(); 
    }, function() { 
     $(".networks", this).hide(); 
     $("h1", this).fadeIn(); 
    }); 
</script> 

<style> 
.networks { 
    display:none; 
} 
</style> 
</head> 

<body> 
<div class="social"> 
    <h1>Share this</h1> 
    <div class="networks"> 
     <p>Twitter</p> 
     <p>Facebook</p> 
    </div> 
</div>  

</body> 
</html> 

当我http://jsfiddle.net/ppksR/尝试,它的真正的工作,但是当我复制粘贴在我的Dreamweaver,它不工作。我错过了什么???任何帮助?

回答

1

jsfiddle正在将JavaScript代码包装到onLoad中,您应该这样做。包裹脚本内容:

$(function() { 
    // your code 
}); 

要具有的jsfiddle模仿你在做什么,改变onLoadNo wrap - in <head>

0

你的问题是基本的jQuery 101 ....元素不存在时,你的代码正在射击。

裹代码document.ready或地点的代码是指

API参考的元素后:http://api.jquery.com/ready/

另外值得一读How jQuery Works在文档为更好的解释。

编辑共振代码在jsfiddle中的工作原因是在左上角的下拉框中显示onLoad

$(function(){ ..... 

}); 

这将确保代码不会执行,直到浏览器读取所有的HTML的:当选择

0

你需要用它在负载处理小提琴自动换行代码。目前您正在定义任何含有.social的元素之前执行代码。

+0

嗨@ Chacha102,我有同样的问题。我试图包裹它,它是这样的: <脚本类型= “文本/ JavaScript的”> \t \t $(函数(){ \t \t $( “社会”。)悬停(函数(){ \t。 \t $( “H1”,此).hide(); \t \t $( “网络。”,这).fadeIn(); “网络” \t \t},函数(){ \t \t $(,这个).hide(); \t \t $(“h1”,this)。淡入(); \t \t}); \t \t}); \t 但仍然没有效果... – 2013-03-04 01:03:03

0

你有没有尝试与其他编辑器/浏览器本地代码?也许DW没有正确加载jQuery。你是否将代码粘贴到纯文本文件并在浏览器中测试该页面?

0

这只是闲置的脚本。

$(document).ready(function(e) { 
$(".social").hover(function() { 
    $("h1", this).hide(); 
    $(".networks", this).fadeIn(); 
    }, function() { 
    $(".networks", this).hide(); 
    $("h1", this).fadeIn(); 
    }); 
}); 

该行$(document).ready(function(e) {使其通过脚本启动onLoad。

这就是你的代码所做的。 http://jsfiddle.net/3SKqQ/