jQuery .hide() 不隐藏元素

Jak*_*ion 2 javascript jquery hide

将 json 解析为指定的跨度标签后,我想自动隐藏英制标签 currentimperial 和 Forecastimperial 但当我尝试执行此操作时

$('#currentimperial').hide();

$('#forecastimperial').hide();
Run Code Online (Sandbox Code Playgroud)

并在 chrome 中查看结果并在控制台中查看,我看到而不是

style="display: none; "
Run Code Online (Sandbox Code Playgroud)

他们实际上有

style="display: inline; "
Run Code Online (Sandbox Code Playgroud)

这是我在 jsBin 上的整个 javascript 和 html http://jsbin.com/efaguv/edit#javascript,html

感谢我收到的任何帮助。

nnn*_*nnn 5

我认为问题是你没有考虑到你的 ajax 调用是异步的这一事实。

正在发生的事情的一个简单得多的示例:

1 jQuery(document).ready(function($) {
2   $('#warning').remove('#warning');   
3   $.ajax({
4        url: 'http://api.wunderground.com/api/APIKEYREMOVED/geolookup/conditions/forecast/q/51.773079,-1.591353.json',
5        dataType: "jsonp",
6        success: function(parsed_json) {
7           $('#currentimperial').fadeIn(1000).append(+temp_f+ '℉ ');
8        }
9    });
10 
11   $('#celcius').hide();
12   $('#currentimperial').hide();
13   $('#forecastimperial').hide();
14 });
Run Code Online (Sandbox Code Playgroud)
  • 第 2 行执行
  • 第 3 行执行以发出 ajax 请求(将在将来的某个时刻收到响应)
  • 第 11-13 行执行以隐藏项目
  • 第 14 行:文档准备功能完成
  • (最终)收到 ajax 响应并调用成功处理程序,因此执行第 7 行。

因为(在完整代码中)ajax 成功回调使元素可见(使用.fadeIn()),最终结果是它们可见。

您可以隐藏 ajax 成功回调中的元素吗?