如何在.Append语句Jquery中使用锚标记

时间:2013-12-05 10:07:43

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

我陷入了需要在附加<li>附加锚点标记链接的情况 但我不知道从哪里开始

这是我的代码:

   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 (address) {
            $.each(address, function () {
                var currVal = this["AddressLine1"];
                var Name = this["Name"];
                var Gender = this["Gender"];
                var Bdate = this["Birthdate"];
                var ID = this["Worker_ID"];
                geocoder.geocode({ 'address': currVal }, function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        var marker = new google.maps.Marker({
                            map: map,
                            icon: iconBase + 'man.png',
                            position: results[0].geometry.location,
                            title: currVal

                        })

                        $('#places').append($('<li>')
                                .text(currVal)
                                .data('location', results[0].geometry.location));

                        google.maps.event.addListener(map, 'bounds_changed', function () {
                            $('#places li').css('display', function () {
                                return (map.getBounds().contains($(this).data('location')))
                      ? ''
                      : 'none';
                            });
                        });
                        function myclick(i) {
                            google.maps.event.trigger(address[i], "click");
                        }

                        //mgr = new MarkerManager(map);


                        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));
                        address.push(marker);

                    }
                    else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
                        setTimeout(codeAddress, 2000);
                    }
                    else {
                        alert("Geocode was not successful for the following reason: " + status);
                    }
                });
            });
            google.maps.event.trigger(map, 'bounds_changed');
        });
    }

但是我试过改变这个但它不起作用

$('#places').append($('<li>')
    .wrap('<a href="javascript:myclick(' + (address.length - 1) + ')">' + location + '</a><br>')
    .data('location', results[0].geometry.location));

它没有显示在列表中。 。 提前谢谢!

1 个答案:

答案 0 :(得分:0)

不确定要实现的目标,但内联事件处理程序不是最佳选择

也许你应该尝试

var j =i;
var link = $('<a href="#">linkTextHere</a>')
            .data('location', results[0].geometry.location);    
$('#places').append($('<li/>').append(link));
link.on('click', function(event){
                     event.preventDefault();
                     google.maps.event.trigger(address[j], "click");
                  }
);

而不是:

$('#places').append($('<li>')
    .wrap('<a href="javascript:myclick(' + (address.length - 1) + ')">' + location + '</a><br>')
    .data('location', results[0].geometry.location));