jQuery die()不起作用 [英] jQuery die() does not work

查看:164
本文介绍了jQuery die()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从创建HTA代码以来,我就被IE困住了:(

Since I'm creating a HTA code I'm stuck with IE :(

我们需要将更改事件捕获在<select>元素中,但是请猜测是什么,IE不支持该事件.

We needed to trap the change event in a <select> element, but guess what, IE does not support that event.

因此,我创建了一种模仿它的方法.单击<input type="text">时,在下面显示<select>.那部分工作正常.问题是,当用户在select之外单击时,我想隐藏select.

So I created a way to mimic it. With a <input type="text"> that when it is being click show the <select> just below. That part works fine. The problem is, I want to hide the select when the user click outside the select.

我试图在body上捕捉到click,它第一次正常工作,但是第二次select隐藏起来,尝试一下.

I tried to catch a click on the body, it works fine the first time but the second time the select gets hidden try away.

这是代码的简化版本:

$('.product').live('click',function(){
    // Show the <select id="select"> code goes here

    // this is the event to close the select
    $('body').die().live('click', function(){ $('#select').fadeOut(250); return;});

    // get the click on the select element
    $('#select').die().live('click',function(){
       // kill the close the select
       // THIS IS THE .die() THAT DOES NOT WORK
       $('body').die();

});

问题 这段代码有什么问题吗?还是有更好的方法来做到这一点?记住,我坚持使用IE.

Question Is there something wrong with this code? OR is there a better way to do this? Remember I'm stuck with IE.

推荐答案

Internet Explorer 7及更高版本(可能是6,因为IE7主要是带有口红的IE6,但我无法轻松地进行测试)做到支持更改事件. 此处是具有非常简单的演示的jsfiddle.

Internet Explorer 7 and up (and probably 6, since IE7 is mostly IE6 with lipstick, but I can't test that easily) do support the change event. Here is a jsfiddle with a really simple demo.

IE在事件中做奇怪的事情是绝对正确的.例如,更改"不会起泡(至少从<select>起,可能还有其他事情起泡),但是jQuery会为您修补.还有一个经典的问题,就是复选框和单选按钮,它们在失去焦点之前不会触发更改"(这基本上使它无用).对于这些用户,我发现点击"效果很好.

It's definitely true that IE does weird things with events. For example, "change" won't bubble (from <select> at least, and probably other things), but jQuery patches over that for you. Also there's the classic issue with checkboxes and radio buttons, which don't fire "change" until they lose focus (which makes it basically useless). For those, I find that "click" works fine.

这篇关于jQuery die()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆