2015-09-23 91 views
1

我想在javascript函数内部使用jquery代码,但它不工作,里面的JavaScript函数有jQuery代码,应该打开弹出窗口。上点击IFRAMEjquery里面的JavaScript函数不工作

工作

<script> 
     window.onload = function() { 
     document.querySelector("iframe").contentWindow.document.body.onclick = function(){ 
      alert("Hello World!"); 
     }; 

     }; 
</script> 

不工作密码

window.onload = function() { 
     document.querySelector("iframe").contentWindow.document.body.<?php echo $event;?> = function(){ 
      var id = '#dialog'; 
      //Get the screen height and width 
      var maskHeight = $(document).height(); 
      var maskWidth = $(window).width(); 

      //Set heigth and width to mask to fill up the whole screen 
      $('#mask').css({'width':maskWidth,'height':maskHeight}); 

      //transition effect 
      $('#mask').fadeIn(500); 
      $('#mask').fadeTo("slow",0.9); 

      //Get the window height and width 
      var winH = $(window).height(); 
      var winW = $(window).width(); 

      //Set the popup window to center 
      $(id).css('top', winH/2-$(id).height()/2); 
      $(id).css('left', winW/2-$(id).width()/2); 

      //transition effect 
      $(id).fadeIn(2000); 
     }; 

}; 
     //if close button is clicked 
     $('.window .close').click(function (e) { 
     //Cancel the link behavior 
     e.preventDefault(); 

     $('#mask').hide(); 
     $('.window').hide(); 
     }); 

     //if mask is clicked 
     $('#mask').click(function() { 
     //alert('ok'); 
     $(this).hide(); 
     $('.window').hide(); 
     }); 

+1

请检查您有错误或不函数是否触发或不 – guradio

+0

检查生成的HTML和分享 - 如果它是在一个浏览器用户查看源代码选项,查看生成的HTML –

+1

有这里还有一段PHP代码:'<?php echo $ event;?'。请检查(并分享)实际输出的代码(因为它在浏览器中),并检查Javascript控制台(F12 - >控制台)以查看是否有错误。 – GolezTrol

回答

0

它看起来像在点击处理程序声明一个错字。有一段看起来不应该在那里的php代码。

document.querySelector("iframe").contentWindow.document.body.<?php echo $event;?> 

将上面的代码替换为要绑定到的事件的实际名称。在这种情况下,onclick事件。

document.querySelector("iframe").contentWindow.document.body.onclick