Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.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 jquery中带选择器的事件名称有什么用途?_Javascript_Jquery_Html_Mouseevent - Fatal编程技术网

Javascript jquery中带选择器的事件名称有什么用途?

Javascript jquery中带选择器的事件名称有什么用途?,javascript,jquery,html,mouseevent,Javascript,Jquery,Html,Mouseevent,我有这个html <div class="wrapper"> <div class="holder"> <span class="pressy">press here..!!</span> <div class="inner"> <span class="pressy">press here..!!</span> </div> </div>

我有这个html

<div class="wrapper">
  <div class="holder">
    <span class="pressy">press here..!!</span>
    <div class="inner">
        <span class="pressy">press here..!!</span>
    </div>
  </div>
</div>
事实上,我对“压力”和“压力”都很警觉,我怎么能只对“内部压力”保持警觉呢

还有一件事是,它的用途是什么

“穆斯镇,内”

(不是在这里。我问的是一般用途)我怎样才能正确使用它

小提琴:


谢谢..

这应该可以满足您的需要:

$('.wrapper').on('mousedown', '.inner .pressy', function (e) {
    alert($(this).attr('class'))
})
在上的
函数中,它忽略了
及其后的任何内容,因此我将
.inner
选择器改为下一个参数,以仅针对您的预期目标。

调用它,使用它以便我们可以分别针对这些事件

假设您正在向按钮添加多个单击处理程序,然后只想删除其中一个处理程序,您是如何做到的?执行
$('button')。关闭('click')
将删除添加到按钮的所有单击处理程序,这不是我们想要的

因此,解决方案是使用名称空间,如

$('button').on('click.myevent', function(){...})
如果我们这样做了

$('button').off('click.myevent')

它将只删除那些与名称空间一起添加的单击处理程序
myevent

上述答案有效,您也可以这样做

 $('.inner').on('mousedown','.pressy',function(e){
    alert($(this).attr('class'))
 })
它作为robs片段的目标是.inner中的pressy类


您只需要像这样修改JS:

$('.wrapper').on('mousedown.inner','.inner .pressy',function(e){
  alert($(this).attr('class'))
})
保持
mousedown.internal
是可选的(正如@Arun所说,它适用于事件名称空间)-在这里没有必要,但不会造成任何伤害。 第二个元素是
.inner.press
y,它是一个更具体的选择器,可以忽略第一个
.pressy
元素


您必须做的唯一更改是指定一个只关心
的委托选择器。在
.internal
中按y
,而不是每个
.internal

$('.wrapper').on('mousedown.inner','.inner .pressy', function(e){
    alert($(this).attr('class'))
});
.on('mousedown.inner'),…
所做的唯一一件事就是为事件处理程序命名名称空间,以便以后可以按名称空间删除它们,而无需删除
.wrapper
上的其他
mousedown
处理程序

$('.wrapper').on('mousedown.inner','.inner .pressy', function(e){
    alert($(this).attr('class'))
});

@kathirvel它唯一的区别在于,它使您能够分离名为
internal
的处理程序,但在事件处理方面没有什么不同。尽管所有这些都是事实,但它并不能回答问题的所有部分。
$('.wrapper').on('mousedown','.inner > .pressy',function(e){
    alert($(this).attr('class'))
    })