Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 文档单击不处理动态元素_Javascript_Jquery_Browser Extension_Crossrider_Kango Framework - Fatal编程技术网

Javascript 文档单击不处理动态元素

Javascript 文档单击不处理动态元素,javascript,jquery,browser-extension,crossrider,kango-framework,Javascript,Jquery,Browser Extension,Crossrider,Kango Framework,内容脚本: var $ = window.$.noConflict(true); // Required for IE function startFunc() { $('a').mouseover(function(e){ var anchor=this; var href=$(anchor).attr('href'); if(href!='#'){ $('.klik-me').remove();

内容脚本:

var $ = window.$.noConflict(true); // Required for IE

function startFunc() {

    $('a').mouseover(function(e){

        var anchor=this;
        var href=$(anchor).attr('href');

        if(href!='#'){

            $('.klik-me').remove();

            const xPos=e.pageX-20;
            const yPos=e.pageY-20;

            let $klikMe=$('<span class="klik-me">Click Me!!</span>').css({
                'padding':'5px',
                'background':'#000',
                'color':'#FFF',
                'font-size':'12px',
                'position':'static',
                'top':yPos,
                'left':xPos,
                'text-align':'center',
                'z-index':999999
            });

            $(anchor).append($klikMe);
        }

    });


}

$('body').on('click','.klik-me',function(){

    const href_in=$(this).parent().attr('href');

    kango.console.log(href_in);

    kango.dispatchMessage('storeHref', {href:href_in});
});


kango.addMessageListener('hrefSuccess', function(event) {

    kango.console.log(event.data.link);

});
kango.addMessageListener('storeHref', function(event) {

event.target.dispatchMessage('hrefSuccess', {link:event.data.href});

});
我正在为页面上的所有锚定标记添加一个弹出窗口(这很好),我在Jquery中添加了一个单击事件(我喜欢这个),并使用
kango.dispatchMessage
将消息发送到后台脚本。似乎什么都没用

任何帮助都将不胜感激


PS:我以前使用过crossrider(很棒)框架

使用下面的代码片段是我到处都在读的,但是我无法让它工作

目前正在尝试做一些相同的事情:

也许它会有一些用处

干杯

$(parent).on('click', child , callback);

我认为正在发生的是单击事件正在冒泡到
元素并触发默认的导航操作

在委托事件处理中,很难停止冒泡/默认行为。相反,我要做的是将
作为
的子项删除。比如说

let $klikMe = $('<span class="klik-me">Click Me!!</span>')
  .css({ ... })
  .data('href', href) // set the href data into the <span>

$(document.body).append($klikMe) // append to body, not anchor

它应该适用于动态元素。:=>$('body')。在('click','klik me',function(){//do something})上;你所说的“似乎什么都没用”到底是什么意思?您浏览器的控制台中是否有任何错误?单击功能时,我正在将父href写入控制台,但在控制台中看不到它您需要尝试阻止单击事件a)冒泡到您的

$(document).on('click', '.klick-me', function() {
  const href_in = $(this).data('href')

  // then continue as in your original code

  return false // the click should have no default action
})