简体   繁体   English

阻止禁用按钮内的图标触发点击?

[英]Prevent icon inside disabled button from triggering click?

Trying to figure out proper way to make a click event not fire on the icon of a disabled link. 试图找出使点击事件不会在禁用链接的图标上触发的正确方法。 The problem is when you click the Icon, it triggers the click event. 问题是当您单击图标时,它会触发单击事件。 I need the selector to include child objects(I think) so that clicking them triggers the event whenever the link is enabled, but it needs to exclude the children when the parent is disabled. 我需要选择器包含子对象(我认为),以便在启用链接时单击它们会触发事件,但是当禁用父对象时,它需要排除子对象。

Links get disabled attribute set dynamically AFTER page load. 页面加载后,链接被禁用的属性会动态设置。 That's why I'm using .on 这就是为什么我使用.on

Demo here: (New link, forgot to set link to disabled) 此处演示:( 新链接,忘记将链接设置为禁用)

http://jsfiddle.net/f5Ytj/9/ http://jsfiddle.net/f5Ytj/9/

<div class="container">
    <div class="hero-unit">
        <h1>Bootstrap jsFiddle Skeleton</h1>
        <p>Fork this fiddle to test your Bootstrap stuff.</p>
        <p>
            <a class="btn" disabled>
                <i class="icon-file"></i>
                Test
            </a>
        </p>
    </div>
</diV>​

$('.btn').on('click', ':not([disabled])', function () { alert("test"); });​

Update: I feel like I'm not using .on right, because it doesn't take the $('.btn') into account, only searching child events. 更新:我觉得我没有使用.on,因为它没有考虑$('。btn'),只搜索子事件。 So I find myself doing things like $('someParentElement').on or $('body').on , one being more difficult to maintain because it assumes the elements appear in a certain context(someone moves the link and now the javascript breaks) and the second method I think is inefficient. 所以我发现自己在做$('someParentElement').on$('body').on事情,这很难维护,因为它假定元素出现在特定的上下文中(有人移动了链接,现在javascript中断),而我认为第二种方法效率低下。

Here is a second example that works properly in both enabled/disabled scenarios, but I feel like having to first select the parent element is really bad, because the event will break if someone rearranges the page layout: 这是第二个示例,在启用/禁用的情况下都可以正常工作,但是我觉得必须首先选择父元素确实很糟糕,因为如果有人重新布置页面布局,该事件将中断:

http://jsfiddle.net/f5Ytj/32/ http://jsfiddle.net/f5Ytj/32/

.on('click', ':not([disabled])'

^ This means that, since the icon is a child of the button ".btn" , and it is not disabled, the function will execute. ^这意味着,由于图标是按钮".btn"的子".btn" ,并且未被禁用,因此该功能将执行。

Either disable the icon, also, or apply the event listener only to the <a> tag that is your button, or use e.stopPropagation(); 也可以禁用该图标,或者仅将事件侦听器应用于作为按钮的<a>标记,或者使用e.stopPropagation();

I would suggest using e.stopPropagation(); 我建议使用 e.stopPropagation(); , this should prevent the icon from responding to the click. ,这样可以防止图标对点击做出响应。

That doesn't seem to work for me ^ 这似乎对我不起作用^

Disabling the icon , however, does. 但是, 禁用图标即可

You're not using the selector properly. 您没有正确使用选择器。

$('.btn').not('[disabled]').on('click', function () { 
    alert("test"); 
});​

See it live here. 在这里看到它。

Edit: 编辑:

$('.container').on('click', '.btn:not([disabled])', function () { 
    alert("test"); 
});​

I would prefer to add the event using delegation here as you are trying to base the event based on the attributes of the element. 我希望在此处使用委托添加事件,因为您尝试基于元素的属性建立事件。

You can add a check condition to see if you want to run the code or not. 您可以添加检查条件以查看是否要运行代码。

$('.container').on('click', '.btn', function() {
    if( $(this).attr('disabled') !== 'disabled'){
        alert('test!!');
    }
});​

Check Fiddle 检查小提琴

Don't use event delegation if you only want to listen for clicks on the .btn element itself: 如果您只想监听.btn元素本身的点击,请不要使用事件委托:

$('.btn').on('click', function() { 
    if (!this.hasAttribute("disabled"))
        alert("test");
});​

If you'd use event delegation, the button would need to be the matching element: 如果要使用事件委托,则按钮必须是匹配的元素:

$(someParent).on('click', '.btn:not([disabled])', function(e) {
    alert('test!!');
});​

Or use a true button , which can really be disabled: 或使用true button ,可以将其禁用:

<button class="btn" [disabled]><span class="file-icon" /> Test</button>

Here, no click event will fire at all when disabled, because it's a proper form element instead of a simple anchor. 在这里,禁用时根本不会触发click事件,因为这是一个适当的表单元素,而不是简单的锚点。 Just use 只需使用

$('.btn').on('click', function() { 
    if (!this.disabled) // check actually not needed
        this.diabled = true;
    var that = this;
    // async action:
    setTimeout(function() {
        that.disabled = false;
    }, 1000);
});​

I think what you need is: 我认为您需要的是:

e.stopPropagation();

See: http://api.jquery.com/event.stopPropagation/ 请参阅: http//api.jquery.com/event.stopPropagation/

Basically something like the following should work 基本上像下面的东西应该工作

$('.icon-file').on('click', function(event){event.stopPropagation();});

You may want to add some logic to only stop bubbling the event when the button ist disabled. 您可能需要添加一些逻辑,以仅在禁用按钮ist时才停止对事件进行冒泡。

Update: 更新:

not sure, but this selector should work: 不确定,但是此选择器应该可以工作:

$('.btn:disabled .icon-file')

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

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