2013-07-03 79 views
0

我有2个图像,他们都在同一个div。我已将一张图片的z-index设置为-1,并使用margin-bottom将其向下移动。Onclick事件不起作用jquery

这是我的代码:

<div data-role="page" id="Mycom" class="background"> 
    <div data-role="header" class="header" data-position="fixed" data-tap-toggle="false"></div> 
    <div data-role="content"> 
     <center> 
       <h2>Headlines</></h2> 

      <div> <a href="#" id="indrotate"><img src="Image/right.png" style="width:30%;z-index:-1;margin-bottom:-30% !important;margin-left:70% !important;" /></a> 

       <img src="SlicedImages/bground2.png" id="indimage" style="height:auto; max-height:80%; width:auto; max-width:90%;" /> 
      </div> 
     </center> 
     <center> <a href="#" data-role="none" data-inline="true" data-transition="none"><img src="Image/Next.png" width="200px" height ="10%"></a> 

     </center> 
    </div> 
</div> 
<!-- ind Page end --> 

,在我的js文件我有:

$(document).on('pagebeforeshow','#Mycom',function(e,data){ 
    $('#indrotate').on('click',function(){ 
       alert("indrotate"); 
    }); 
}); 

什么错误,我在做什么?

+0

你尝试过'pageshow'吗? – Satpal

+0

你不需要将它绑定到页面事件。只要删除'pagebeforeshow'绑定。 – Omar

回答

1

当jQuery Mobile的单击事件工作应始终秉行与委托事件绑定:

$(document).on('click','#indrotate',function(){ 
      alert("indrotate"); 
}); 

这就是所谓的委托事件绑定。它不关心元素是否已经存在于DOM内部。它的工作原理是因为事件绑定到像文档对象这样的静态元素,并且只有当它存在于DOM内时才会传播到正确的元素。

工作jsFiddle例如:http://jsfiddle.net/Gajotres/LDLmF/

还有一个其他的可能性。如果您使用多个HTML页面,而这不是第一页。如果是这种情况,并且它的javascript在HEAD中,它将被丢弃并且不被执行。阅读更多关于它在这ARTICLE,或找到它HERE

+0

感谢您的答案。但我有一个圆形的图像,顶部的图像完全来自圆形图像的边界。我想以这种方式修复图像。由于顶部图像与圆形图像的边界一起出现,因此'on('click',function())'方法不起作用。 – Beginner