2016-12-29 104 views
-2

我有HTML:jQuery的淡入淡出和不工作

var $button = $('#trigger'); 
 
    \t var $to_top = $('#to_top'); 
 
    \t $button.on('click', function() { 
 
    \t \t $to_top.slideToggle(); 
 
    \t });
#to_top { 
 
    \t position: fixed; 
 
    \t bottom: 20px; 
 
    \t right: 20px; 
 
    \t background-color: #9B9EA0; 
 
    \t float: right; 
 
    \t width: 56px; 
 
    \t height: 56px; 
 
    \t fill: white; 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="to_top"> 
 
    \t <svg> 
 
    \t \t <path d="M16.652 37.273l13.102-12.045L42.86 37.273l3.796-2.563-16.902-15.534L12.858 34.71"/> 
 
    \t </svg> 
 
    </div> 
 
    <button id="trigger">FadeIt</button>

的问题是,当我按一下按钮没有任何反应。我的代码有什么问题。

+2

现在看到你的问题,并告诉什么是行不通的? – ab29007

+1

我想,你需要Jquery库的参考。 @kittyCat新增 – Vikrant

+0

是的....但在我的网站上它不工作,我的控制台上没有任何东西出现。 – bashkovpd

回答

-2

在文档“准备就绪”之前,不能安全地操作页面。 jQuery为您检测到这种状态。包含在$(document).ready()中的代码只有在页面文档对象模型(DOM)准备好执行JavaScript代码时才会运行。包含在$(window).load(function(){...})中的代码将在整个页面(图像或iframe)(而不仅仅是DOM)准备就绪后运行。

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

该构造已经在我的代码中。 – bashkovpd

+0

请使用[编辑]链接说明此代码的工作原理,不要只给代码,因为解释更有可能帮助未来的读者。另见[回答]。 –

0

这应该工作,请注意您使用的幻灯片效果不会褪色。

$(document).ready(function() { 
    var $button = $('#trigger'); 
    var $to_top = $('#to_top'); 
    $button.on('click', function() { 
     $to_top.slideToggle(); 
    }); 
});