2011-11-15 46 views
0
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
    <title>Assignment 1</title> 
    <style> 
     .detail { padding: 5px;} 
     .hidden {display:none;} 
     .unhidden {display: block;} 
    </style> 
</head> 
<body> 
    <div class="container"> 
     <ul class="list"> 
      <li> 
       <div class="master">Pre-foundation</div> 
       <div class="hidden"> 
        XYZ....... 
       </div> 
      </li> 
      <li> 
       <div class="master">Apple I</div> 
       <div class="hidden"> 
        XYZ..... 
       </div> 
      </li> 
      <li> 
       <div class="master">Apple II</div> 
       <div class="hidden"> 
        XYZ..... 
       </div> 
      </li> 
      <li> 
       <div class="master">Apple III</div> 
       <div class="hidden"> 
        XYZ..... 
       </div> 
      </li> 
      <li> 
       <div class="master">Apple IPO</div> 
       <div class="hidden"> 
        XYZ..... 
       </div> 
      </li> 
     </ul> 
    </div> 
    <script type="text/javascript"src="/Users/Documents/JS/jquery-1.6.2.js"> 
    jQuery.noConflict(); 
    function show_alert() 
    { 
    alert("x! I am an alert box!"); 
    } 
    show_alert(); 
    $(".master").click(function() { 
     alert ("hello"); 
    }); 
</script> 
</body> 

無法得到處理的類標籤

我有問題獲得點擊功能工作。我試圖在用戶點擊段落標題時展開隱藏文字。

jquery-1.6.2.js文件的來源是正確的,我可以在瀏覽器中打開它。它還顯示在螢火蟲的「腳本」標籤下。 如果我從代碼中刪除了src =「/ Users/Documents/JS/jquery-1.6.2.js」,我可以看到一個從show_alert();開始的警告框。但是當我在代碼中添加「src = ..」時show_alert();停止工作,我無法處理'主'級。我究竟做錯了什麼?

+0

什麼樣的錯誤你在如果任何控制檯的故事嗎? –

+1

我不認爲你可以指定一個腳本標籤的源代碼並在其中包含內容。但我可能是錯的。 –

+0

我沒有得到任何錯誤,只是無法抓住「主」標籤手柄。 – user988639

回答

0

我認爲主要問題是你沒有用jQuery 'ready' function包裝你的代碼。它需要被包裹在:

$(document).ready(function() {}); 
or 
$(function() {}); 

由於您使用$.noConflict()你需要使用的,而不是它的「$」的別名jQuery的它被稱爲後:

$(function() { 
    jQuery.noConflict(); 
    function show_alert() 
    { 
     alert("x! I am an alert box!"); 
    } 
    show_alert(); 
    jQuery(".master").click(function() { 
     alert ("hello"); 
    }); 
}); 

Here就是一個的jsfiddle所有的工作。

1

您還沒有等待DOM準備就緒。

$(function() { 

    // Execute any jQuery code here 

}); 
0

您有兩個問題。

  • 首先,如@Interstellar_Coder所述,您應該有兩個單獨的腳本標記。一個加載外部jQuery,另一個加載你的代碼。

  • 其次,你打電話jQuery.noConflict(),它放棄控制$變量。將$更改爲jQuery,只要您的jQuery文件路徑有效,它就會工作。


<script type="text/javascript" src="/Users/Documents/JS/jquery-1.6.2.js"></script> 
<script type="text/javascript"> 
    jQuery.noConflict(); 

    function show_alert() { 
     alert("x! I am an alert box!"); 
    } 
    show_alert(); 

    <!-- no dollar sign --> 
    jQuery(".master").click(function() { 
     alert ("hello"); 
    }); 
</script>