内部DataTable initComplete
$('#' + key.toLowerCase().replace(/\s/g, "-") + '-table').prepend("<div class='toolbar'>" + createButtonBar(year) + "<label id='categorySelection' style='padding-top: 5px;'>Select Category:</label><select style='margin-left:5px;margin-right: 10px;margin-left: 5px;' class='form-control input-sm' id='categorySelectBox'/>");
DataTable外部的功能
function createButtonBar(years) {
var htmlString = "";
years = years.reverse();
htmlString += "<div id='#above1200buttonbar'>stuff</div>"
years.forEach(function(entry) {
htmlString += "<label><input name='date' type='radio' value='" + entry + "'>" + entry + "</label>";
});
$('#above1200buttonbar').replaceWith(htmlString);
console.log(htmlString);
}
来自console.log()的
<div id='#above1200buttonbar'><div class='hide-below-1200 btn-group' style='padding: 5px 0 5px 0;' data-toggle='buttons'><label class='btn btn-default btn-sm active year-btn'><input name='date' type='radio' id='mybuttons' value='all'>All</label><label class='btn btn-default btn-sm year-btn'><input name='date' type='radio' value='2016'>2016</label><label class='btn btn-default btn-sm year-btn'><input name='date' type='radio' value='2015'>2015</label>
问题是我的构建的html没有被添加到DOM之前。
var cleanKey = $('#' + key.toLowerCase().replace(/\s/g, "-") + '-table');
我这样做是为了防止在复制,粘贴或编辑时出错
答案 0 :(得分:1)
$( "#above1200buttonbar" ).append(htmlString);
试试