2016-02-26 88 views
-1

我和朋友正在網站上工作,我們無法弄清楚如何讓JSFiddle在網頁上工作。如何讓Jsfiddles在網站上工作?

問題是,代碼在JSFiddle上工作,它無縫地工作。但是,當我們在頁面上上傳相同的內容時,我們看不到任何操作。在下面,你可以找到我們的小提琴和我們的頁面。

這件事讓我們目前瘋狂,我們不確定我們錯過了什麼。 小提琴鏈接:https://jsfiddle.net/n53qg/177/ 小提琴HTML:

<div> 
<a class="link" href="#" data-rel="content1">Link 1</a> 
<a class="link" href="#" data-rel="content2">Link 2</a> 
<a class="link" href="#" data-rel="content3">Link 3</a> 
<a class="link" href="#" data-rel="content4">Link 4</a> 
<a class="link" href="#" data-rel="content5">Link 5</a> 
</div> 
<div class="content-container"> 
<div id="content1">This is the test content for part 1</div> 
<div id="content2">This is the test content for part 2</div> 
<div id="content3">This is the test content for part 3</div> 
<div id="content4">This is the test content for part 4</div> 
<div id="content5">This is the test content for part 5</div> 
</div> 

小提琴JS:

$(".link").click(function(e) { 
    e.preventDefault(); 
    $('.content-container div').fadeOut('slow'); 
    $('#' + $(this).data('rel')).fadeIn('slow'); 
}); 

小提琴CSS:

.content-container { 
position: relative; 
width: 400px; 
height: 400px; 
} 
.content-container div { 
display: none; 
position: absolute; 
top: 0; 
left: 0; 
right: 0; 
bottom: 0; 
} 

我們的頁面:http://178.62.254.14/test/denemecik.html

回答

3

的問題是你的script是在頁面的其餘部分被加載之前執行。你有兩個選擇:

  1. 把你script標籤後的所有內容(在body年底)

  2. 包裝你的代碼在DOM就緒功能:

    $(document).ready(function() { 
        //your code in here 
    }); 
    

默認情況下,JSFiddle代碼在DOM被處理後加載,因此它在那裏工作的原因而不是您的網站。