2015-09-24 176 views
0

我有了這個HTML在我的SharePoint 2010 Web部件項目的* WebPartUserControl.ascx文件:爲什麼我的jQuery點擊處理程序不能觸發?

<html> 
    <h1>Pre- and Post Travel Form Help</h1> 
    <div id="preTravel" name="preTravel"> 
    <h2>Pre Travel</h2> 
    <img id="imgPreTravel" name="imgPreTravel" src="/_layouts/images/TravelFormHelp/posttravelpdf.png" alt="pre Travel image" height="275" width="350"> 
    </div> 
    <div id="postTravel" name="postTravel"> 
    <h2>Post Travel</h2> 
    <img id="imgPostTravel" name="imgPostTravel" src="/_layouts/images/TravelFormHelp/posttravelpdf.png" alt="post Travel image" height="275" width="350"> 
    </div> 
</html> 

...這jQuery的:

<script type="text/javascript"> 
    $('imgPostTravel').click(function() { 
     alert('you clicked the post Travel image'); 
     this.addClass('hide'); 
    }); 
</script> 

相關的CSS是:

.hide { 
    visibility: hidden; 
    display: none; 
} 

當我點擊'imgPostTravel'時,雖然沒有任何反應 - 不僅圖像不會消失,我看不到警報。

作爲完整性檢查(這我可能沒有),我試過類似的代碼中的jsfiddle here那裏,太單擊處理在不觸發 - 必須有某種根本性愚蠢約我在做什麼,但我無法看到它... ...

+5

你需要使用'$('#imgPreTravel')'而不是'$('imgPreTravel')' – Sushil

回答

7

這是一個簡單的修補程序,只需添加一個主題標籤,以您的選擇,因爲它是一個ID:

$('#imgPostTravel').click(function() { 
+0

廢話,我簡直不敢相信我再次忘記了;有人踢我(不是一件好事)。 –

+0

總是很好,得到另一雙眼睛!請點擊答案上的勾號以接受它。 – mdeang2

1

改變這樣

<script type="text/javascript"> 
     $(function() { 
      $('#imgPostTravel').click(function() { 
       alert('you clicked the post Travel image'); 
       this.addClass('hide'); 
      }); 
     }); 
</script> 
代碼
相關問題