2016-12-29 93 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(); 
    }); 
});