【问题标题】:Removing `<li>` with appended anchor tag leaves style attributes删除带有附加锚标记的 `<li>` 会留下样式属性
【发布时间】:2014-01-30 02:30:14
【问题描述】:

我有一个问题阻止我继续前进 3 天

我有一个附加了锚标记的列表,但是当我删除它时,锚标记更改为 style:none 并且 &lt;li&gt; 样式仍然存在。

当标记超出地图边界时,我在 Google 地图上使用它。我希望删除包含锚标记的整个&lt;li&gt;,但删除锚标记而不是&lt;li&gt;

这是我的代码。

var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
function codeAddress() {
    infoBubble = new InfoBubble({
        map: map,
        shadowStyle: 0,
        padding: 10,
        borderRadius: 10,
        arrowSize: 15,
        maxWidth: 300,
        borderWidth: 1,
        borderColor: '#ccc',
        arrowPosition: 50,
        arrowStyle: 0
    });
    $.getJSON('/Dashboard/LoadWorkerList', function Geocode(addresses) {
        $.each(addresses, function () {
            var currVal = this["AddressLine1"];
            var Name = this["Name"];
            var Gender = this["Gender"];
            var Bdate = this["Birthdate"];
            var ID = this["Worker_ID"];
            var Latitude = this["Latitude"];
            var Longitude = this["Longitude"];
            var LatLang = new google.maps.LatLng(Latitude, Longitude);

            var marker = new google.maps.Marker({
                map: map,
                icon: iconBase + 'man.png',
                position: LatLang,
                title: currVal
            })

            var link = $('<a href="#">' + currVal + '</a>')
                 .data('location', LatLang);
            $('#places').append($('<li id=\'List\' class=\'List\'>').append(link));
            link.on('click', function (event) {
                event.preventDefault();
                google.maps.event.trigger(addresses[0], "click");
                infoBubble.removeTab(0);
                infoBubble.addTab(Name, "Name: " + Name + "<br> Address: " + currVal + "<br> Gender: " + Gender + "<br> Birthdate: " + Bdate + "<br><br>" + '<center><a href="/Worker/WorkerDetails?workerId=' + ID + '">View Profile</a></center>');
                infoBubble.open(map, marker);
            });

            google.maps.event.addListener(map, 'idle', function () {
                    $('#places').find('li a').css('display', function () {
                        return (map.getBounds().contains($(this).data('location')))
                  ? ''
                  : 'none';
                    });
                });



            google.maps.event.addListener(marker, 'click', (function (marker, i) {
                return function () {
                    infoBubble.removeTab(0);
                    infoBubble.addTab(Name, "Name: " + Name + "<br> Address: " + currVal + "<br> Gender: " + Gender + "<br> Birthdate: " + Bdate + "<br><br>" + '<center><a href="/Worker/WorkerDetails?workerId=' + ID + '">View Profile</a></center>');
                    infoBubble.open(map, marker);
                }
            })(marker, currVal));
            addresses.push(marker);

        })
        google.maps.event.trigger(map, 'bounds_changed');
    })
}

当它加载时,查看第二张图片,注意样式仍然保留

样式

<style>
    .List
    {
        background-color: #f1f1f1;
        padding: 10px;
        overflow: hidden;
    }
    .List:nth-child(odd)
    {
        background-color: #fcfcfc;
    }
</style>

【问题讨论】:

  • 为此我们需要周围的代码。您的脚本在这里没有上下文,因此它可以在for() 循环内执行,在页面加载期间独立执行,附加到事件处理程序(如click)或其他东西。请发布您的 Javascript 和 CSS 的其余部分。
  • @Deryck 添加了其余的 javascript
  • @Deryck 更新了 CSS 和新的 JS

标签: javascript jquery html css google-maps-api-3


【解决方案1】:

旧:

$('#places li').css('display', function () {
    return (map.getBounds().contains($(this).data('location'))) ? '' : 'none';
});

新功能:

$('#places > li').each(function() {
    var inside = (map.getBounds().contains($(this).find('a').data('location'))) ? '' : 'none';
    $(this).css('display', inside);
});

试试这个

【讨论】:

  • 先生,我只是想澄清一下,我已将 $('#places li').css('display',function(){ return(map.getBounds().contains($(this).data('location')))?'' : 'none';}); 更改为 google.maps.event.addListener(map, 'idle', function () { $('#places').find('li a').css('display', function () { return (map.getBounds().contains($(this).data('location'))) ? '' : 'none'; }); });
  • 如果你这样做,你只会改变你的&lt;a&gt;。您需要选择li,而不是li a。如果以某种方式对您有用,那就太好了。但如果没有,请尝试我的修复。
  • 是的.. 就是这样,它只改变了我的&lt;a&gt;,不包括&lt;li&gt;
  • 你能编辑适合编辑代码的答案吗?
  • 因此,当您使用刚刚在上面对此答案的第一条评论中键入的代码时,它会更改 &lt;a&gt; 而不是 &lt;li&gt;?
猜你喜欢
  • 1970-01-01
  • 2011-07-27
  • 1970-01-01
  • 1970-01-01
  • 2015-05-09
  • 2013-10-21
  • 1970-01-01
  • 1970-01-01
  • 2013-06-27
相关资源
最近更新 更多