简体   繁体   English

JS定位错误的对象

[英]JS targeting wrong object

Here is the fiddle:这是小提琴:

https://jsfiddle.net/0a0yo575/10/ https://jsfiddle.net/0a0yo575/10/

I am trying to display one box, in the top-left, at a time depending on what is active.我试图在左上角显示一个框,具体取决于活动的内容。 But at the moment the JS is removing the point not the box?但目前 JS 正在删除点而不是框?

$('.red-point').click(function() {
    $('.infobox').removeClass('hidn').addClass('show');
    $(this).removeClass('show').addClass('hide');
});

In your current code $(this) refers to the marker you have clicked.在您当前的代码中$(this)指的是您单击的标记。 So when you say $(this).removeClass('show').addClass('hide');所以当你说$(this).removeClass('show').addClass('hide'); you are hiding the clicked marker.您正在隐藏单击的标记。

If you give your .infobox elements a class that matches the id of the clicked marker...如果你给你的.infobox元素一个与点击标记的id匹配的class ......

<div class="infobox hide termini">Bar<br>Termini</div>

...you could toggle them like this... ......你可以像这样切换它们......

$('.red-point').click(function() {
    $('.infobox').removeClass('show').addClass('hide');
    $('.' + $(this).attr('id')).addClass('show');
});

JSFiddle Example JSFiddle 示例


Personally, I would give each marker a data attribute containing the desired infowindow text and use this to populate a single infowindow ...就个人而言,我会给每个标记一个包含所需infowindow文本的数据属性,并使用它来填充单个infowindow ......

<div class="infobox hide"></div>

<div class="abs red-point" id="termini" data-description="Bar<br>Termini">
     <a onClick="turnGreen(event)">
        <span class="num">1</span>
    </a>
</div>

$('.red-point').click(function() {
    $('.infobox').removeClass('hide').addClass('show').html($(this).data('description'));
});

JSFiddle Example JSFiddle 示例

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

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