简体   繁体   English

为什么通过带有gmap事件点击监听器的jQuery将自定义类添加到主体时,为什么我的自定义类未应用于主体?

[英]Why my custom class is not applied to body when adding it through jQuery with gmap event click listener?

I want to open a slide menu when clicking a gmap marker, but I can't. 单击gmap标记时,我想打开幻灯片菜单,但不能。 Here is how I proceeded to achieve that: 这是我如何实现这一目标的方法:

google.maps.event.addListener(marker, 'click', function () {  
    $('body').toggleClass("side-panel-open");
    if( $('body').hasClass('device-touch') ) {
       $('body').toggleClass("ohidden");
    }
    return false;
});

The same thing is working well with a normal button, when I manage that this way: 当我以这种方式管理按钮时,同样的事情也可以正常使用:

$("#test").click(function(){
    $('body').toggleClass("side-panel-open");
    if( $('body').hasClass('device-touch') ) {
        $('body').toggleClass("ohidden");
    }
    return false;
});

As you can see there: 如您所见:

http://www.themes.krown.ch/canvas4/HTML/index-fullscreen-image_test.html http://www.themes.krown.ch/canvas4/HTML/index-fullscreen-image_test.html

The "TEST" button on the footer, is working perfectly. 页脚上的“测试”按钮运行正常。 Unfortunately, when clicking the gmap marker, nothing happens. 不幸的是,当单击gmap标记时,没有任何反应。 I can see on Firebug that something happens on body, but the class "side-panel-open" doesn't stay. 我在Firebug上看到身体上发生了某些事情,但是类“ side-panel-open”并没有消失。

I also tried to just add a class to the body, this way: 我还尝试通过这种方式将一个类添加到主体:

$('body').addClass('side-panel-open');

Nothing happens as with the first code. 与第一个代码一样,没有任何反应。 This is so strange because if I try to add another class, for exemple "class_name". 这很奇怪,因为如果我尝试添加另一个类,例如“ class_name”。 This class will be applied as expected to the body: 此类将按预期应用于身体:

$('body').addClass('class_name');

Documentation: https://developers.google.com/maps/documentation/javascript/examples/event-simple 文档: https : //developers.google.com/maps/documentation/javascript/examples/event-simple

For the button, you can use the jQuery click event or if you're using a different javascript framework, they will likely have a click event as well. 对于按钮,您可以使用jQuery click event如果您使用的是其他JavaScript框架,则它们也可能也会具有click事件。 For jQuery, you would do the following: 对于jQuery,您需要执行以下操作:

$('#boundaries-states').click(function () {
//Do something
});

It looks like the addListener method takes the google Map object, not the HTML element for the map. 看起来addListener方法采用google Map对象,而不是地图的HTML元素。 Take a look at the first example here. 在这里看第一个例子。 Programs interested in certain events will register JavaScript event listeners for those events and execute code when those events are received by calling addListener() to register event handlers on the object. 对某些事件感兴趣的程序将为这些事件注册JavaScript事件侦听器,并在通过调用addListener()在对象上注册事件处理程序而接收到这些事件时执行代码。

So if you're not already, you'll need to hold onto the reference to your map that is returned when you instantiate the map: 因此,如果还没有,则需要保留实例化地图时返回的对地图的引用:

var map = new google.maps.Map(document.getElementById('boundaries-states'),
mapOptions);
Once you have your map reference, you can add a listener using the map object, rather than the HTML element.

google.maps.event.addListener(map, 'click', function() {
alert('clicked');
});

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

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