2010-07-08 131 views
0

我已經在我的本地服務器上編寫了這段代碼(我在Mac上安裝了Mac 2上的Apache 2),但它沒有運行。jQuery不能在我的本地服務器上運行

JavasSript在Safari或Firefox上處於活動狀態,但它沒有對這些進行處理。 這段代碼更糟嗎?或者我可以嘗試其他方式?請幫忙。

<html> 
<head> 
    <title> jquery test </title> 
    <script src="http://www.google.com/jsapi"></script> 
    <script> 
    // Load jQuery 
    google.load("jquery", "1"); 
    </script> 

    <script type="text/javascript"> 
    JQuery(function($){ 
    var $curr = $(".sel"); 
    $("button").click(function(){ 
     $curr.removeClass("sel"); 
     $curr.$curr.prev().addClass("sel") 
    }); 
    }); 
    </script> 
    <style type="text/css"> 
    span { padding :8px;} 
    .sel { border :orange solid 4px;} 
    </style> 
     </head> 
     <body> 
    <p> 
    <span>1</span> 
    <span>2</span> 
    <span class="sel">3</span> 
    <span>4</span> 
    <span>5</span> 
    <button>click</button> 
    </p> 
</body> 
</html> 

回答

4

應該是jQuery,而不是JQuery,你會得到一個JavaScript錯誤調用了一個不存在:)

一個變量時,也該有一個額外的$curr

$curr.$curr.prev().addClass("sel") 

它應該是:

$curr.prev().addClass("sel") 

You can see a version with both of these fixes here


如果你總是想搬回一個,你需要的,而不是總是指的是有class="sel",像這樣的原始元素你選擇移動點擊裏面:

jQuery(function($){ 
    $("button").click(function(){ 
    $(".sel").removeClass("sel").prev().addClass("sel"); 
    }); 
});​ 

You can test it here

+0

謝謝您的建議!有用!我誤以爲「jQuery」咒語。 而jsfiddle很酷。 – Shunter1112 2010-07-09 00:40:06

0

爲了幫助您進行調試,safari和firefox都有javascript錯誤/輸出日誌。在Firefox上按下控制鍵切換j打開控制檯,在這裏你可以看到出了什麼問題。

+0

感謝您的幫助。我也想用js很酷的調試方式,但我沒有。 awasome! – Shunter1112 2010-07-09 00:41:09

相關問題