【问题标题】:Track click over iframe by Javascript / Jquery通过 Javascript / Jquery 跟踪 iframe 上的点击
【发布时间】:2017-06-24 17:47:17
【问题描述】:

我们如何跟踪 iframe 上的点击(如任何 advt。)并根据网站用户分别使用 advt id 和用户 id 在我自己的数据库(使用 ajax)中记录该事件

  1. 我在父 div 上尝试过 onclick(如下所示)事件,但没有工作 -

            $('div.ad').on('click', function(){
            	$.ajax({
            		url: 'clickevent.php',
            		type: 'POST',
            		data: {
            			'id': adId
            		},
            		dataType: 'json',
            		success: function(data){
            			// show success msg to user
            		},
            		error: function(){
            			// show failure msg
            		}
            	});
            });
  2. 我尝试在光标位于 iframe 上方时进行聚焦和模糊

        <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
        <script>
        jQuery(function( $ ){
                var isOverAd = false;
                $( "iframe[ id *= google ]" )
                    .mouseover(function(){
                            isOverAd = true;
                        })
                    .mouseout(function(){
                            isOverAd = false;
                        });
                $( window ).blur(
                    function(){
                        if (isOverAd){
                            $.ajax({
                                url: 'clickevent.php',
                                type: 'POST',
                                data: {
                                    'id': adId
                                },
                                dataType: 'json',
            					success: function(data){
            						//show success msg
            					},
            					error: function(){
            						//show failure msg
            					}
                            });
                        }
                    }).focus();
            });
  3. 我也尝试过当鼠标在 iframe 上时窗口丢失模糊
  4. 我已映射像素($0 值)它也无法正常工作。

但没有任何效果......如果你能帮助我,我将非常感激

【问题讨论】:

  • 这个错误表明你没有在页面中添加jquery文件。
  • @FarzinKanzi 不,实际上我在我的项目中做过,但在这里我只发布了我的代码
  • 所以你创建的代码sn-p是没用的。

标签: javascript php jquery html iframe


【解决方案1】:

要访问页面中的框架,您可以这样做:

var frame = window.frames['FRAME-ID'];
$(frame).load(function ()
{
    $(frame).contents().find('ELEMENT-IN-FRAME');
    //To manage click on element body ( or any elements):
    $(frame).contents().find('body').on('click', function(){alert('k')});

【讨论】:

  • 但是我怎么知道用户点击了该 iframe 或该框架内的任何链接以在我的数据库中注册该事件?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-09
  • 1970-01-01
  • 1970-01-01
  • 2017-08-10
相关资源
最近更新 更多