简体   繁体   English

使用onclick无法获得正确的$(this)元素。 尝试使用:not在div内输入

[英]Can't get the right $(this) element with onclick. Trying to use :not for input inside a div

That was one hard title to write with a good explanation. 那是一个很难解释的标题。

I got the following html: 我得到以下html:

 <div class="requestItem clearfix">
       <div class="col-sm-2">Great info goes here yo.</div>
       <div class="col-sm-2">Some info goes here</div>
       <div class="col-sm-2">Date goes here.</div>
       <div class="col-sm-1 pull-right">
            <div class="buttons">
                <div class="row">
                    <input type="button" value="Edit" class="btn dark-button tiny-button"  />
                    <input type="button" value="Delete" class="btn dark-button tiny-button margin-top" />
                    </div>
                </div>
            </div>
            <div class="row extraInfo">
                <div class="col-sm-12"> 
                   some random info
            </div>
        </div>  
  </div>

with the following JS: 使用以下JS:

$('.requestItem').click(function () {
    console.log($(this));
    if ($(this).is(':input')) {
        alert("input clicked"); return;
    } else { $(this).children('.extraInfo').slideToggle(); }
});

What should happen: When i click one of the buttons which is of element input, the toggle should not happen. 应该发生什么:当我单击元素输入的按钮之一时,不应发生切换。

What happens: $(this) is always the parent div, requestItem. 会发生什么:$(this)始终是父div requestItem。 I get that this can be a normal problem since the requestItem is wrapped around the button, however, i need to know if the button was clicked since i don't want to toggle a slide then. 我知道这可能是一个正常问题,因为requestItem包裹在按钮周围,但是,我需要知道是否单击了按钮,因为我不想再切换幻灯片。

Thanks in advance! 提前致谢!

AFTER HELP: This is what i ended up with: Hope it helps someone else too. 之后的帮助:这就是我最终得到的:希望它也能帮助其他人。

$('.requestItem').click(function (e) {
    if (!$(e.target).is(':input')) {
        $(this).children('.extraInfo').slideToggle();
    }
});

You should use event.target 您应该使用event.target

Working jsfiddle: http://jsfiddle.net/pr7e9wqq/1/ 工作的jsfiddle: http : //jsfiddle.net/pr7e9wqq/1/

Basically, since your listener is on the containing element div.requestitem , that is the this context for the callback function. 基本上,由于您的侦听器位于包含元素div.requestitem ,因此回调函数的this上下文。 Your click event on the input bubbled up to the containing element (which you were listening on). 输入上的click事件冒泡到包含元素(您正在监听)。 Remember that this will always be set to the element that matches your selector. 请记住, this将始终设置为与选择器匹配的元素。

By default, this in a jQuery click handler returns is the element that it's bound to (in this case, .requestItem ). 默认情况下, this在jQuery click处理程序中返回的是它绑定到的元素(在本例中为.requestItem )。 To get the element that got clicked, use e.target : 要获取被点击的元素,请使用e.target

$('.requestItem').click(function (e) {
    console.log($(e.target));
    if ($(e.target).is('input')) {
        alert("input clicked"); return;
    } else { $(this).children('.extraInfo').slideToggle(); }
});

Fiddle: http://jsfiddle.net/c6zbwq03/ 小提琴: http : //jsfiddle.net/c6zbwq03/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM