使用Jquery跟踪iframe内的点击次数 [英] Track clicks inside iframe with Jquery
问题描述
我正试图找出一种用jquery跟踪iframe内部点击的方法。
iframe位于同一个域名中,但我似乎无法完成这项工作,这是我的代码:
< HTML>
< head>
< script type =text / javascriptsrc =http://code.jquery.com/jquery-1.6.2.min.js>< / script>
< script type =text / javascript>
var clicks = 0;
$('#myframe')。load(function(){
alert('loaded');
var that = $(this);
that.contents() .find('a')。bind('click',function(e){
e.preventDefault();
点击++;
$('#clicks')。html(点击次数) );
});
});
< / script>
< / head>
< body>
点击次数:< span id =clicks> 0< / span>
< iframe id =myframesrc =test_iframe.phpwidth =500height =500>< / iframe>
< / body>
< / html>
任何可能出错的想法?
<我在SO看到了与此问题非常相似的其他问题,但没有一个问题解决了我的问题。
更新:
我在 http://www.politicos.biz/stack/iframe_click.php ,它适用于jsfiddle但不在我的网站上。
在父框架上,创建一个从iframe内部调用的函数。
父母:
var click = 0;
函数addClick()
{
click ++;
}
然后,从iframe调用它
parent.addClick();
最后,简单参考点击
获取点击次数。希望这会有所帮助。
I'm trying to figure out a way of tracking clicks inside an iframe with jquery. The iframe is locate on the same domain but still I don't seem to be able to make this work, here's my code:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
var clicks = 0;
$('#myframe').load(function() {
alert('loaded');
var that = $(this);
that.contents().find('a').bind('click', function(e) {
e.preventDefault();
clicks++;
$('#clicks').html(clicks);
});
});
</script>
</head>
<body>
Clicks: <span id="clicks" >0</span>
<iframe id="myframe" src="test_iframe.php" width="500" height="500"></iframe>
</body>
</html>
Any idea of what could be wrong ?
I've saw other questions very similar to this one at SO but none of the answers solved my problem.
UPDATE:
I've updated my code at http://www.politicos.biz/stack/iframe_click.php with @Wes code, it works on jsfiddle but not on my site.
on the parent frame, create a function to be called from inside the iframe.
parent:
var click = 0;
function addClick()
{
click++;
}
then, call it from the iframe
parent.addClick();
finally, simple refer to click
for the number of click count. Hope this helps.
这篇关于使用Jquery跟踪iframe内的点击次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!