2015-06-22 34 views
0

如何使用jquery將事件放在iframe中的對象上。iframe中的對象上的事件

我的iFrame HTML:

<iframe name="users" width="220" height="510" align="left" 
src='messages/users.php'></iframe> 

在這IFRAME我有一個鏈接

<a class="link" href=""></a> 

所以我嘗試寫的jQuery腳本,做一些動作,當你點擊這個鏈接 我試圖使用.contents().find("body").onclick,但它不起作用,可能我犯了某個地方的錯誤, 甚至試圖使用只是

$(document).on('click',".link", function() 
     {}); 
  • 但defenetly desn't工作,因爲它是iframe的 可能是我需要javscript parent.document.getElementById使用DOM不知何故,什麼樣的?

user.php的

 <html> 
<head> 
<meta http-equiv="Content-type" content="text/html; charset=UTF-8"> 
<meta content="10; url=users.php?login=<?=$login;? "HTTP-EQUIV=Refresh> 
</head> 
<body> 
    <?php 
printf(" <p><a href=\"\" class=\"link\" id=\"".$line."\">".$row['Login']."</a></p>");?> 
</body> 
</html> 
+0

負載功能$('#用戶)問題負載(函數(){ \t警報( 「這裏」);});不給我結果 –

回答

0

您需要使用jQuery load method,使用下面的代碼和嘗試。 。

$(function() { 
$('iframe').load(function() { 
    $(this).contents().find("body") 
     .on('click', function(event) { 
      alert('test'); 
    }); 
}) 
}) 
+0

不行,這是不給我任何結果 –