2015-09-23 135 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