jsbook icon indicating copy to clipboard operation
jsbook copied to clipboard

第296页,第11章,11.15 全新的实现

Open RubyLouvre opened this issue 11 years ago • 0 comments

去掉onselectionchange的实现 后面改为

现在我们回过头来搞定IE9。在IE9下,onpropertychange不好用了,惨遭阉割,不支持回退键、粘贴复制操作的监听。因此我们需要多管齐下,结合多种事件来模拟此事件了(在KISSY、YUI等框架,它们还动用到定时器)
下面的代码取自avalon.js,只要将当中的updateVModel改成你的回调函数就可用。
// https://github.com/RubyLouvre/avalon/blob/1.2.6/avalon.js#L2883 
if (document.addEventListener && document.documentMode !== 9) { //IE10+, W3C
    element.addEventListener("input", updateVModel)
    data.rollback = function() {
        element.removeEventListener("input", updateVModel)
    }
} else {
    var eventArr = ["keyup", "paste", "cut", "change"]
    removeFn = function(e) {
        var key = e.keyCode
        //    command            modifiers                   arrows
        if (key === 91 || (15 < key && key < 19) || (37 <= key && key <= 40))
            return
        updateVModel()
    }
    eventArr.forEach(function(name) {
        element.attachEvent("on" + name, removeFn)
    })
}



RubyLouvre avatar Apr 17 '14 09:04 RubyLouvre