有没有可能动态创建Bootstrap按钮?我有一个文本文件,其中列出了一些项目,我将使用JavaScript创建一个数组。我希望用这些项目作为每个按钮的文本来动态创建Bootstrap按钮。如果文本文件中有10个项目,则会创建10个按钮。有人可以告诉我如何实现或指向一些相关教程吗?
编辑(现在可以创建,但无法检查是否已创建按钮):
createButtons():
编辑(现在可以创建,但无法检查是否已创建按钮):
createButtons():
$(function() {
$.ajax({
url : 'http://localhost:8080/SSAD/type.txt',
dataType : "text",
success : function (filecontent) {
var lines=filecontent.split('\n');
$.each(lines, function() {
if (this!='') {
var word = this;
word = word.toLowerCase().replace(/(?:_| |\b)(\w)/g, function(str, p1) { return p1.toUpperCase();});
if ($('button:contains("'+word+'")').length==0) {
var button='<button type="button" class="btn btn-block btn-inverse active" data-toggle="button tooltip" title="Click this to enable/disable viewing of'+this+'" onclick="toggleVisibility('+"'"+this+"'"+')">'+word+'</button>';
$(".crisisButtons").append(button);
}
}
});
}
});
});
HTML:
<button type="button" class="btn btn-block btn-inverse" onclick="createButtons">Click me!</button>
<div class="crisisButtons"></div>