Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.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 阻止事件通过div JQuery中的选择菜单传播_Javascript_Jquery - Fatal编程技术网

Javascript 阻止事件通过div JQuery中的选择菜单传播

Javascript 阻止事件通过div JQuery中的选择菜单传播,javascript,jquery,Javascript,Jquery,我有一点问题要解决。基本上,我正在尝试将事件侦听器绑定到div中的一些select菜单。这是我的代码(每个下拉菜单几乎相同) 我遇到的问题是,当我在div中单击上面的任何select菜单时,事件似乎会通过每个元素向下移动,直到事件的最后一个事件处理程序 a.prog\u添加类。我想阻止这种情况发生。我试过e.stopPropagation(),但似乎没有效果 我很感激在这方面的任何帮助,因为我真的不知道下一步该做什么 event.stopPropagation()的文档说明: 由于.live()

我有一点问题要解决。基本上,我正在尝试将事件侦听器绑定到div中的一些
select
菜单。这是我的代码(每个下拉菜单几乎相同)

我遇到的问题是,当我在div中单击上面的任何select菜单时,事件似乎会通过每个元素向下移动,直到事件的最后一个事件处理程序
a.prog\u添加类。我想阻止这种情况发生。我试过
e.stopPropagation()
,但似乎没有效果


我很感激在这方面的任何帮助,因为我真的不知道下一步该做什么

event.stopPropagation()的文档说明:

由于.live()方法在事件传播到文档顶部后处理事件,因此无法停止实时事件的传播。类似地,由.delegate()处理的事件将传播到它们被委派到的元素;在调用委托的事件处理程序时,绑定在DOM树中它下面的任何元素上的事件处理程序将已经执行。因此,这些处理程序可以通过调用event.stopPropagation()或返回false来防止委托处理程序触发

此语句在一段时间之前应该已经更新,以包括对
.on(事件、选择器、处理程序)
语法的提及,该语法是
.live()
/
.delegate()
的替代,并在版本1.7中引入


您唯一的选择是不委托-即直接将处理程序附加到相关元素。

是否尝试返回false以停止传播?是,这也不起作用。从jQuery 1.7开始,.on()方法是将事件处理程序附加到文档的首选方法。如果您创建一个JSFIDLE,我们将能够提供更多帮助。我理想情况下希望这样做,唯一的问题是我动态创建这些元素,以便它们在第一次加载时不在页面上,因此我很难直接附加它们。实际上,再考虑一下,您可以测试
event.delegateTarget==event.currentTarget
。当不相等时,事件冒泡,当相等时,事件没有冒泡。我接受了这个答案,因为它为我指出了正确的方向,并提到了我最后用来解决问题的
live()
方法。我知道它从1.9版就被删除了,但我需要一个快速修复,这就成功了。谢谢@Roamer-1888@Javacadabra,谢谢你的慷慨接受。我在最后一句话中用了,因为你显然找到了另一种方式。
$('#compresult').bind('click', 'a.con_add', function(e){
            e.stopPropagation();
            console.log('choose this contact');
            $contact_name = $('#comp_staff option:selected').text();
            $contactId = $('#comp_staff option:selected').val();
            $type = 'contact';
            $('#item_name').html(" - " + $contact_name);
            $('#comp_name').val($contact_name);
            $('#comp_id').val($contactId);
            $('#type').val($type);
            $('#apply_tags').show();
            $('#active_tags').show();
            $('#doneBtn').show();
            $('#tag_results').load('pages/ajax/query_tags.php', {'query':'A', 'id':$contactId, 'type':$type});
            $('#active_tags').load('pages/ajax/get_tags.php', {'name': $contact_name, 'id': $contactId, 'type': $type},function(response, status, xhr){});
        });

        //Event Listener for the COLUMNS Dropdown.
        $('#compresult').bind('click', 'a.col_add', function(e){
            e.stopPropagation();
            console.log('choose this column');
            $column_name = $('#comp_cols option:selected').text();
            $columnId = $('#comp_cols option:selected').val();
            $type = 'column';
            $('#item_name').html(" - " + $column_name);
            $('#comp_name').val($column_name);
            $('#comp_id').val($columnId);
            $('#type').val($type);
            $('#apply_tags').show();
            $('#active_tags').show();
            $('#doneBtn').show();
            $('#tag_results').load('pages/ajax/query_tags.php', {'query':'A', 'id':$columnId, 'type':$type});
            $('#active_tags').load('pages/ajax/get_tags.php', {'name': $column_name, 'id': $columnId, 'type': $type},function(response, status, xhr){});
        });

        //Event Listener for the SUPPLEMENTS Dropdown.
        $('#compresult').bind('click', 'a.sup_add', function(e){
            e.stopPropagation();
            console.log('choose this Supplement');
            $supplement_name = $('#comp_sups option:selected').text();
            $supplementId = $('#comp_sups option:selected').val();
            $type = 'supplement';
            $('#item_name').html(" - " + $supplement_name);
            $('#comp_name').val($supplement_name);
            $('#comp_id').val($supplementId);
            $('#type').val($type);
            $('#apply_tags').show();
            $('#active_tags').show();
            $('#doneBtn').show();
            $('#tag_results').load('pages/ajax/query_tags.php', {'query':'A', 'id':$supplementId, 'type':$type});
            $('#active_tags').load('pages/ajax/get_tags.php', {'name': $supplement_name, 'id': $supplementId, 'type': $type},function(response, status, xhr){});
        });

        //Event Listener for the PROGRAMMES Dropdown.
        $('#compresult').bind('click', 'a.prog_add', function(e){
            e.stopPropagation();
            console.log('choose this Programme');
            $programme_name = $('#comp_progs option:selected').text();
            $programmeId = $('#comp_progs option:selected').val();
            $type = 'supplement';
            $('#item_name').html(" - " + $programme_name);
            $('#comp_name').val($programme_name);
            $('#comp_id').val($programmeId);
            $('#type').val($type);
            $('#apply_tags').show();
            $('#active_tags').show();
            $('#doneBtn').show();
            $('#tag_results').load('pages/ajax/query_tags.php', {'query':'A', 'id':$programmeId, 'type':$type});
            $('#active_tags').load('pages/ajax/get_tags.php', {'name': $programme_name, 'id': $programmeId, 'type': $type},function(response, status, xhr){});
        });