我有了这个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和那里,太单击处理在不触发 - 必须有某种根本性愚蠢约我在做什么,但我无法看到它... ...
你需要使用'$('#imgPreTravel')'而不是'$('imgPreTravel')' – Sushil