简体   繁体   English

简单点击show()什么都不做

[英]Simple click show() does nothing

I might be losing my mind here: 我可能会在这里失去理智:

<span class="showMap" style="cursor:pointer;">(Show map)</span>

<div class="col-md-12">
    <div id="map" style="height:300px; display:none;"></div>
</div>

As you can see, DIV #map is set to not display, on clicking DIV .showMap I want it to show: 如您所见,DIV #map设置为不显示,点击DIV .showMap我希望它显示:

$( ".showMap" ).click(function() {
    alert('hey'); // for testing -> works perfectly 
    $('#map').show();
});

But it does nothing. 但它没有任何作用。 No errors, nothing in console. 没有错误,没有在控制台中。

The hidden DIV contains a google map, not sure if that makes any difference but thought I'd mention it. 隐藏的DIV包含一个谷歌地图,不知道这是否有任何区别,但我想我会提到它。

Google Maps does not like being hidden when it is being loaded. Google地图不喜欢在加载时隐藏。 Think of jquery's height() function - it will return a wrong value when the element is hidden. 想想jquery的height()函数 - 隐藏元素时它会返回错误的值。

I'm not sure this will work, but using CSS have you #map as position:absolute;left:-9999em; 我不确定这会起作用,但是使用CSS有#map作为position:absolute;left:-9999em; so that it appears off-screen. 所以它出现在屏幕外。 This might allow Google Maps to load correctly. 这可能允许Google地图正确加载。 On the click function you will then want to hide() the map element, change the css to position:relative;left:0; 在click函数上,您将要hide() map元素,将css更改为position:relative;left:0; and then show() it. 然后show()它。

jQuery(function($){
  var map = $('#map');
  map.css({position:'absolute',left:'-9999em'});
  $('.showMore').click(function(){
    map.hide().css({position:'relative',left:'0em'}).show();
  });
});

works for me: http://jsfiddle.net/snowburnt/q392B/ 适合我: http//jsfiddle.net/snowburnt/q392B/

put something in the div, then you'll see it open. 把东西放在div中,然后你会看到它打开。

<div class="col-md-12">
    <div id="map" style="height:300px;display:none;background-color:green">hello</div>
</div>

it doesn't look like there is anything there, because the background is white and there's nothing in the DIV. 看起来没有任何东西,因为背景是白色的,DIV中没有任何东西。 (also, as answered above, google maps doesn't like not being displayed). (另外,如上所述,谷歌地图不喜欢不显示)。 So your code is working, just not displaying anything. 所以你的代码工作正常,只是没有显示任何内容。

Try this... JsFiddle 试试这个... JsFiddle

 $(document).ready(function(){
      $( ".showMap" ).click(function() {
        alert('hey'); // for testing -> works perfectly 
        $('#map').css('display','');
    });    
});

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

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