简体   繁体   English

jQuery .show()-Chrome与IE 11

[英]jQuery .show() - Chrome vs. IE 11

I'm using this code within a jQuery AJAX` request: 我在jQuery AJAX`请求中使用以下代码:

beforeSend: function() {
    loading.show();
},
complete: function(){
    loading.hide();
}

loading looks this way: var loading = form.siblings("img") . loading看起来是这样的: var loading = form.siblings("img")

And the css for loading is: loadingcss是:

display: none;
margin: 10px auto;
width: 30px;
height: auto;

The code works just fine with a minor inconsistency between Chrome and IE 11: 该代码在Chrome和IE 11之间存在少许不一致的情况下可以正常工作:

  • in Chrome the .show() adds display: block to the loading element allowing the .gif to be centered: 在Chrome中, .show()display: block添加到loading元素,以使.gif居中: 在此处输入图片说明
  • in IE 11 the same method adds display: inline : 在IE 11中,相同的方法添加display: inline 在此处输入图片说明

Is there any way to alter this behavior directly within jQuery without having to force .css("display", "block") after the .show() method? 有什么方法可以直接在jQuery更改此行为,而不必在.show()方法之后强制使用.css("display", "block")吗? And what would be the pitfalls of doing so? 这样做的陷阱是什么?

Thank you very much! 非常感谢你!

Try substituting .css(propertyName, function) for .show() , .hide() 尝试用.css(propertyName, function) .show() .hide()

var toggle = function toggle() {
  loading.css("display", function(index, display) {
    return display === "none" ? "block" : "none"
  })
};

beforeSend: function() {
  toggle()
},
complete: function(){
  toggle()
}

 $(function() { var loading = $("img"); var toggle = function toggle() { loading.css("display", function(index, display) { return display === "none" ? "block" : "none" }) } $("button").click(function() { toggle() }) }) 
 img { display: none; margin: 10px auto; width: 16px; height: auto; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> </script> <button>click</button> <img src="data:image/gif;base64,R0lGODlhEAAQAPIAAP///wAAAMLCwkJCQgAAAGJiYoKCgpKSkiH+GkNyZWF0ZWQgd2l0aCBhamF4bG9hZC5pbmZvACH5BAAKAAAAIf8LTkVUU0NBUEUyLjADAQAAACwAAAAAEAAQAAADMwi63P4wyklrE2MIOggZnAdOmGYJRbExwroUmcG2LmDEwnHQLVsYOd2mBzkYDAdKa+dIAAAh+QQACgABACwAAAAAEAAQAAADNAi63P5OjCEgG4QMu7DmikRxQlFUYDEZIGBMRVsaqHwctXXf7WEYB4Ag1xjihkMZsiUkKhIAIfkEAAoAAgAsAAAAABAAEAAAAzYIujIjK8pByJDMlFYvBoVjHA70GU7xSUJhmKtwHPAKzLO9HMaoKwJZ7Rf8AYPDDzKpZBqfvwQAIfkEAAoAAwAsAAAAABAAEAAAAzMIumIlK8oyhpHsnFZfhYumCYUhDAQxRIdhHBGqRoKw0R8DYlJd8z0fMDgsGo/IpHI5TAAAIfkEAAoABAAsAAAAABAAEAAAAzIIunInK0rnZBTwGPNMgQwmdsNgXGJUlIWEuR5oWUIpz8pAEAMe6TwfwyYsGo/IpFKSAAAh+QQACgAFACwAAAAAEAAQAAADMwi6IMKQORfjdOe82p4wGccc4CEuQradylesojEMBgsUc2G7sDX3lQGBMLAJibufbSlKAAAh+QQACgAGACwAAAAAEAAQAAADMgi63P7wCRHZnFVdmgHu2nFwlWCI3WGc3TSWhUFGxTAUkGCbtgENBMJAEJsxgMLWzpEAACH5BAAKAAcALAAAAAAQABAAAAMyCLrc/jDKSatlQtScKdceCAjDII7HcQ4EMTCpyrCuUBjCYRgHVtqlAiB1YhiCnlsRkAAAOwAAAAAAAAAAAA=="> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM