2012-06-04 96 views
0

我是新來的jQuery,目前閱讀jQuery的假人,並試圖執行本書的第一個例子:警告框不會彈出

<!DOCTYPE html PUBLIC 「-//W3C//DTD XHTML 1.0 Strict//EN」 
「http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd」> 
<html> 
<head> 
<title>My Test Page</title> 
<script type="text/javascript" src="js/jquery-1.7.2.js"> 
$(document).ready(function(){ 
alert(jQuery(‘img’).attr(‘alt’)); 
}); 
</script> 
</head> 
<body> 
<p>This is my test page.</p> 
<img src= "images/home.gif" height="28" width="28" alt="This is a test 
image."> 
</body> 
</html> 

但在執行這個例子後,警報框不彈出。

+0

可能'jQuery的1.7.2.js '在本地目錄中沒有加載或丟失。 – Bob

+0

對'jQuery('img')。attr('alt'));'使用雙引號或單引號''而不是那些有趣的 –

+0

我沒有注意到有趣的。 :) – Bob

回答

1
<script type="text/javascript" src="js/jquery-1.7.2.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function(){ 
    alert($('img').attr('alt')); 
    }); 
</script> 

這應該是訣竅。

+0

+1擁有正確類型的**引號**使世界有所不同。 – arttronics

0

嘗試改變

<script type="text/javascript" src="js/jquery-1.7.2.js"> 
$(document).ready(function(){ 
alert(jQuery(‘img’).attr(‘alt’)); 
}); 
</script> 

<script type="text/javascript" src="js/jquery-1.7.2.js"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 
    alert(jQuery(‘img’).attr(‘alt’)); 
}); 
</script> 
+0

那些報價呢?做他們的工作? –

+0

@WouterH。不,他們沒有。 – gdoron

+0

@WouterH因爲JavaScript太聰明的報價:) – VisioN

0

你打算在多個問題。

首先,你的例子顯示'聰明的引號'('/')。這些並不是計算世界中任何事物所理解的真實的引號。

其次,您嘗試錯誤地使用腳本標記。

你需要將它們分解成多個標記,一個用於嵌入,以及一個用於在線:

<script type="text/javascript" src="js/jquery-1.7.2.js"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 
    alert(jQuery('img').attr('alt')); 
}); 
</script> 
+0

thnx約翰......現在的工作...... – Abhinav

1

您使用的怪異引號(‘ ’): 警報(jQuery的( 'IMG' ).attr( 'ALT'));

alert(jQuery('img').attr('alt')); 

或者用雙引號:

alert(jQuery("img").attr("alt")); 

而且你不能src屬性編寫一個腳本標籤中的javascript:

<script type="text/javascript" src="js/jquery-1.7.2.js"> </script> 
<script type="text/javascript"> 
    $(document).ready(function(){ 
     alert(jQuery('img').attr('alt')); 
    }); 
</script> 
+0

他們並不奇怪,他們很聰明: ) – VisioN

+0

@VisioN。 [行情](http://en.wikipedia.org/wiki/Quotation_mark_glyphs)。我想他們對於JavaScript來說太「聰明」了......':))' – gdoron

+0

@gdoron:thnx ... – Abhinav