knockout.js中判断IE版本的非常巧妙的办法

    // Detect IE versions for bug workarounds (uses IE conditionals, not UA string, for robustness)
    // Note that, since IE 10 does not support conditional comments, the following logic only detects IE < 10.
    // Currently this is by design, since IE 10+ behaves correctly when treated as a standard browser.
    // If there is a future need to detect specific versions of IE10+, we will amend this.
    var ieVersion = document && (function() {
        var version = 3, div = document.createElement('div'), iElems = div.getElementsByTagName('i');
        // Keep constructing conditional HTML blocks until we hit one that resolves to an empty fragment
        while (
            div.innerHTML = '<!--[if gt IE ' + (++version) + ']><i></i><![endif]-->',
            iElems[0]
        ) {}
        return version > 4 ? version : undefined;
    }());
    var isIe6 = ieVersion === 6,
        isIe7 = ieVersion === 7;


转载于:https://my.oschina.net/yck108/blog/373030

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Knockout.js 是一个基于 MVVM 架构的 JavaScript 框架,它可以帮助我们实现数据绑定、模板渲染、事件绑定等功能。要显示上传文件的进度,我们可以使用 XMLHttpRequest 对象来发送文件,并使用 Knockout.js 来更新进度条的值。 首先,我们需要定义一个 ViewModel,用来绑定页面上的元素和数据: ``` function ViewModel() { var self = this; self.uploadProgress = ko.observable(0); self.uploadFile = function() { var fileInput = document.getElementById("fileInput"); var file = fileInput.files[0]; var xhr = new XMLHttpRequest(); xhr.upload.addEventListener("progress", function(event) { if (event.lengthComputable) { var percentComplete = event.loaded / event.total * 100; self.uploadProgress(percentComplete); } }, false); xhr.open("POST", "/upload"); xhr.send(file); }; } ko.applyBindings(new ViewModel()); ``` 在这个 ViewModel ,我们定义了一个 uploadProgress 可观察的属性,它用来绑定页面上的进度条。我们还定义了一个 uploadFile 方法,用来上传文件。在上传文件的过程,我们使用 XMLHttpRequest 的 upload 事件来监听文件上传的进度,如果进度发生变化,我们就更新 uploadProgress 的值,从而更新页面上的进度条。 在 HTML 代码,我们可以使用 Knockout.js 的数据绑定语法来绑定 uploadProgress 属性,以显示进度条: ``` <input type="file" id="fileInput"> <button data-bind="click: uploadFile">Upload File</button> <div class="progress"> <div class="progress-bar" role="progressbar" style="width: 0%;" data-bind="style: { width: uploadProgress() + '%' }"> <span data-bind="text: uploadProgress() + '%'"></span> </div> </div> ``` 在这个 HTML 代码,我们使用了 Bootstrap 的进度条样式,并使用了 Knockout.js 的数据绑定语法来绑定 uploadProgress 属性。当 uploadProgress 的值发生变化时,进度条的宽度和文本也会相应地更新。 总结一下,要显示上传文件的进度,我们可以使用 XMLHttpRequest 对象来发送文件,并使用 Knockout.js 来更新进度条的值。我们可以定义一个 ViewModel,用来绑定页面上的元素和数据,定义一个 uploadFile 方法,用来上传文件,使用 XMLHttpRequest 的 upload 事件来监听文件上传的进度,如果进度发生变化,就更新 uploadProgress 的值,从而更新页面上的进度条。在 HTML 代码,我们可以使用 Knockout.js 的数据绑定语法来绑定 uploadProgress 属性,以显示进度条。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值