这里我有一个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>';
但是这个WONT可以工作了。我如何解决我的问题,并将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();
}
});
});
答案 0 :(得分:0)
如果要在页面加载后将按钮添加到DOM ,则需要使用事件委派来绑定事件处理程序。例如:
$(function() {
$('body').on('click', '#my-button', function() {
alert('The button was clicked!);
})
});
请发布您的其余代码。