有谁知道IE'返回false'? [英] Does anyone know if IE 'return false'?

查看:91
本文介绍了有谁知道IE'返回false'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个简单的测试代码适用于IE以外的所有浏览器(在IE8中测试):

This simple test code works in all browsers except IE (tested in IE8):

#dBox {
    height:100px;
    width: 230px;   
    overflow-y:auto;    
}

$(function () { 
    $('#s').focus(function(){  
        $(this).after('<ul id="dBox"></ul>');  
        for (i=0;i<10;i++) $('#dBox').append('<li>'+i+'</li>');
    });
    $('#dBox').live('mousedown',function(event) {  
        //event.stopPropagation();  
        //event.preventDefault();  
        //console.log  (event.isDefaultPrevented());  
        //event.stopImmediatePropagation();  
        return false;   
     });  
    $('#s').blur(function () { $('#dBox').remove();  });
});     
<input type="text" id="s"/>

我尝试了现场fn的所有选项,但没有运气。

On event.isDefaultPrevented()它返回true,但是 #dBox 它仍然被移除。

我甚至试图在焦点fn中绑定一个常规的mousedown。但是同样的结果它在所有浏览器中工作但不是IE。

有没有人知道什么是错的?

I've tried all the options from live fn., but no luck.
On event.isDefaultPrevented() it returns true, but #dBox it's still removed.
I've even tried to bind a regular mousedown inside the focus fn. But the same result it's working in all browsers but not IE.
Does anyone have any idea of what's wrong?

推荐答案

这是直接因为事件委派与 jQuery live()绑定事件使用live不绑定直接元素。相反,它在文档的根节点上附加一个处理程序。

This is directly because of Event Delegation with jQuery live() binding events using live does not bind the direct element. Instead it attaches a handler on the root node of the document.


.live()方法能够影响
元素尚未通过使用事件
委托向DOM添加
:绑定到
祖先元素的处理程序负责在$ b上触发的
事件$ b后代。传递给
.live()的处理程序从不绑定到元素;
而不是.live()将一个特殊的
处理程序绑定到DOM树的根目录。
在我们的示例中,当单击新元素
时,会发生以下步骤:

The .live() method is able to affect elements that have not yet been added to the DOM through the use of event delegation: a handler bound to an ancestor element is responsible for events that are triggered on its descendants. The handler passed to .live() is never bound to an element; instead, .live() binds a special handler to the root of the DOM tree. In our example, when the new element is clicked, the following steps occur:

假设您要单击#dBox元素时保持对输入的关注,这将起作用,这是一个演示 < a href =http://www.jsfiddle.net/WurDE/ =nofollow> http://www.jsfiddle.net/WurDE/

Assuming you want to keep focus on the input when the #dBox element is clicked this will work and here is a demo http://www.jsfiddle.net/WurDE/

注意:如果您不想保持对输入的关注,请从代码中删除触发器('focus')

Note: if you do not want to retain focus on the input then remove the trigger('focus') from the code.

$(function () {
    $('#s').focus(function(){
        if (!$('#dBox').length) {
          var $dbox = $('<ul id="dBox"></ul>');
          for (i=0;i<10;i++) $dbox.append('<li>'+i+'</li>');
          $(this).after($dbox);
          $dbox.bind("mousedown", function() {
            $('#s').unbind('blur')
          });
          $dbox.bind("mouseup", function() {
            $('#s').bind('blur', function () {
              $('#dBox').remove();  
            }).trigger('focus');
          });
        }
    });

    $('#s').bind('blur', function () {
      $('#dBox').remove();  
    });
});  

这篇关于有谁知道IE'返回false'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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