简体   繁体   English

触发下一个事件,但之后不触发

[英]Fire next event up, but not after that

For a page, I've been given a link wrapped inside a label, like this: 对于页面,已经给了我一个包装在标签内的链接,如下所示:

<label for='checkbox_elem'>
    Links to <a href='somepage.php' target='anotherwindow'>another page.</a>
</label>

When the user clicks on the link in all browser, the page is spawned in a new tab as envisioned, but in Firefox the checkbox linked to the label is also selected. 当用户单击所有浏览器中的链接时,页面将按预期在新选项卡中生成,但是在Firefox中,还选中了链接到标签的复选框。 This is a non-desired behavior. 这是不希望的行为。

I want to run some jQuery to allow the link to pop, but to kill the event thereafter. 我想运行一些jQuery以允许链接弹出,但此后将终止该事件。 I have the following, which works, but isn't very elegant: 我有以下方法,可以使用,但不是很优雅:

$(document).ready(function(){
    $('label a').click(function(e){
        open($(this).attr('href'), $(this).attr('target'));
        return false;
    });
});

Can any of you think of a more elegant way to do this than to replicate the element's behavior manually and kill the event? 与手动复制元素的行为并杀死事件相比,你们中谁能想到一种更优雅的方法呢?


As an aside, I've been trying w/ stopPropagation , but haven't had much success. stopPropagation ,我一直在尝试w / stopPropagation ,但没有取得太大的成功。

Also, please note that the above solution does work , but I am looking for something more elegant for generically stopping events from propagating past their first call. 另外,请注意,上述解决方案确实有效 ,但是我正在寻找一种更优雅的方法,用于通常阻止事件传播到它们的第一个电话之外。 (The first native call. If I add a callback, I still want the native element behavior to fire, but not that of its parent) . (第一个本机调用。如果添加回调,我仍然希望触发本机元素行为,而不是其父项行为)。

Thanks, Joe 谢谢乔

Try stopping the event from propagating at the label. 尝试阻止事件在标签上传播。

$(document).ready(function(){
    $('label').click(function(e){
        e.stopPropagation();
    });
});

你可以使用$(ELEM)。一(FN) - http://api.jquery.com/one/

Maybe you should change your html to: 也许您应该将html更改为:

<label for='checkbox_elem'>Links to </label>
<a href='somepage.php' target='anotherwindow'>another page.</a>

and use no javascript at all? 并完全不使用JavaScript?

Well, if you are allowed to change the html on client-side, you can always move the links outside the label like this, 好吧,如果允许您在客户端更改html,则可以始终将链接移动到标签之外,例如,

$(document).ready(function(){ 
    $('label').find('a').each(function(){
        var $anchor = $(this);        
        $(this).closest('label').after($anchor);
    });
});​

demo 演示

This one: 这个:

<label> test <a href="#">Test</a> <input type="checkbox" name="check[]" /></label>

Or even: 甚至:

<label for="test1"> test <a href="#">Test</a> <input type="checkbox" name="test1" id="test1" class="checkbox" /></label>

And js: 和js:

$('a').click(function(e){
    var t=$(this);
    window.open(t.attr('href'), t.attr('target'));
    return false;
});

Works for me. 为我工作。 Maybe there is smth that interfere with your script? 也许有些东西会干扰您的脚本?

Try using .one() instead of .click() : 尝试使用.one()而不是.click()

$(document).ready(function(){
    $('label a').one(function(e){
        open($(this).attr('href'), $(this).attr('target'));
        return false;
    });
});

KISS

$(document).ready(function(){
    $('label a').click(function(e){
        open($(this).attr('href'), $(this).attr('target'));
        $(this).parent().click(); //Click the label again to reverse the effects of the first click.
        return false;
    });
});

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

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