2015-09-24 122 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> 
代码
相关问题