-1

这里我有一个 contentStr HTML 代码,我必须像这样在 jquery dialog() 中调用它:

$(contentStr).dialog({
              modal:true
          });

但问题是因为我在 contentStr 中有一个按钮 HTML,我需要在点击时运行函数,所以我尝试:

    contentStr += '<button'+ onclick=addContent(); +'>Add to timeline</button>';

和:

        contentStr += '<button onclick="addContent()">Add to timeline</button>';

但这不会起作用。我如何解决我的问题并将按钮 html 添加到 contentStr 并在单击时运行函数 addContent() ???

更新:

google.maps.event.addListener(marker,'click',function(){

        service.getDetails(request, function(place, status) {
          if (status == google.maps.places.PlacesServiceStatus.OK) {
            //class m_tooltip
            var contentStr = '<div><h5>'+place.name+'</h5><p>'+place.formatted_address+'</p>';
            if (!!place.photos) contentStr += '<img src='+place.photos[0].getUrl({ 'maxWidth': 500, 'maxHeight': 500 })+'></img>';
            if (!!place.formatted_phone_number) contentStr += '<br>'+place.formatted_phone_number;
            if (!!place.website) contentStr += '<br><a target="_blank" href="'+place.website+'">'+place.website+'</a>';

            //if (!!place.opening_hours.periods[1].open.time) contentStr += '<br>'+place.opening_hours.periods[1].open.time;
            if (place.reviews && place.reviews.length) for(var i=0;i<place.reviews.length;i++){
            contentStr += '<br>'+ place.reviews[i].text+ '</br>';
            }       
            contentStr += '<p>'+distanca/1000+'km</p>';
            //var pvreme = vreme / 60;
            contentStr += '<p>'+vreme+'</p>';

            var cons = google.maps.geometry.spherical.computeDistanceBetween(startMarkerPosition,endMarkerPosition);
            var mnozilac = distanca/cons;

            var stop = place.geometry.location;
            var between = google.maps.geometry.spherical.computeDistanceBetween(startMarkerPosition,stop)/1000*mnozilac;
            var time = between/110*60;
            var time = Math.floor(time);
            var zajson = formatTime(time);
            contentStr += '<p>'+ parseFloat(between).toFixed(2) + 'km</p>';
            contentStr += '<p>'+ formatTime(time) + '</p>';

            contentStr += '<button>Add to timeline</button></div>';
            //contentStr += '<div><gcse:search></gcse:search></div></div>';


            $(contentStr).dialog({
              modal:true
          });
            //contentStr += '<h5">'+place.reviews[0].text+'</h5></div>';
            //ib.setContent(contentStr);
            //ib.open(map,marker);

          } else { 
            var contentStr = "<h5>No Result, status="+status+"</h5>";
            //ib.setContent(contentStr);
            //ib.open(map,marker);
           $(contentStr).dialog();
          }  
        });

    });
4

1 回答 1

0

如果在页面加载后将按钮添加到 DOM,则需要使用事件委托来绑定事件处理程序。例如:

$(function() {

   $('body').on('click', '#my-button', function() {
       alert('The button was clicked!);
   })

});

请发布您的其余代码。

于 2013-09-23T00:52:28.290 回答