因为ayer.confirm的按钮并非采用button,而是a标签,所以获取按钮焦点获取不到,要采用别的方法,下面介绍在ie11中和ie8中不同的写法
在ie11中
- layer.confirm('确定取消这个弹窗吗?',{
- btn: ['确定', '取消'],
- success:function(layero){
- //获取焦点
- var btn = layero[0].getElementsByClassName('layui-layer-btn')
- [0].getElementsByTagName('A')[0];
- btn.href = 'javascript:void(0)';
- btn.focus();
- },
- title:"提示"
- },function(index){
- layer.close(index);
- //你的操作
- });
- })
在ie8中不支持getElementsByClassName方法,我们需要对它做额外的处理
- layer.confirm('确定取消这个弹窗吗?',{
- btn: ['确定', '取消'],
- success:function(layero){
- //获取焦点
- var btn = getElementsByClassName('layui-layer-btn')
- [0].getElementsByTagName('A')[0];
- btn.href = 'javascript:void(0)';
- btn.focus();
- },
- title:"提示"
- },function(index){
- layer.close(index);
- //你的操作
- });
- })
重写getElementsByClassName方法
- var getElementsByClassName = function (searchClass, node,tag) {
- var result = [];
- node = node || document;
- tag = tag || "*";
- var classes = searchClass.split(" "),
- elements = (tag === "*" && node.all) ? node.all: node.getElementsByTagName(tag),
- patterns = [],
- current,
- match;
- var i = classes.length;
- while (--i >= 0) {
- patterns.push(new RegExp("(^|\\s)" + classes[i] + "(\\s|$)"));
- }
- var j = elements.length;
- while (--j >= 0) {
- current = elements[j];
- match = false;
- for (var k = 0,
- kl = patterns.length; k < kl; k++) {
- match = patterns[k].test(current.className);
- if (!match) break;
- }
- if (match) result.push(current);
- }
- return result;
- }
后种方法是兼容的不怕麻烦的可直接采取后种方法