我正在尝试使用类似于(https://www.w3schools.com/jquery/jquery_dom_add.asp)
的jQuery向特定HTML元素添加按钮我的问题是,当我尝试运行代码时,
var txt1 = "<button type = "button" class = "btn btn-primary">text</button>";
$("#thing1").append(txt1); // Append new elements
我没有得到我想要的样式,除非我像上面那样做,否则它会弄乱我的代码的另一部分,除非没有类似的引导样式,
var txt1 = "<button">text</button>";
$("#thing1").append(txt1); // Append new elements
第二个例子可以正常工作,除了按钮样式不像我在第一个例子中想要的那样使用bootstrap并且不确定为什么?
下面是更完整的代码图片,
<script>
function askName(){
var name = prompt("What's your name?");
var message = "Hello " + name + ", would you like to build a table?"
document.getElementById('output').innerHTML = message;
var txt1 = "<button type = "button" class = "btn btn-primary">text</button>";
$("#thing1").append(txt1); // Append new elements
};
function tables(){
var txt1 = "<p>Text.</p>"; // Create text with HTML
var txt2 = $("<p></p>").text("Text."); // Create text with jQuery
var txt3 = document.createElement("p");
txt3.innerHTML = "Text."; // Create text with DOM
$("body").append(txt1, txt2, txt3); // Append new elements
}
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({
left: '250px',
opacity: '0.5',
height: '150px',
width: '150px'
});
});
});
</script>
</head>
<body>
<div class="container-fluid">
<button type = "button" class= "btn btn-primary" onclick="askName()">
Want to chat?
</button>
</div>
<h3 style = "text-align: center"class="text-primary" id="output"></h3>
<div id = thing1>
</div>
</body>
</html>
答案 0 :(得分:1)
我认为你有一个问题,你应该混合你的&#34;和你的&#39;
试试这个:
var txt1 = "<button type = 'button' class = 'btn btn-primary'>text</button>";
答案 1 :(得分:1)
问题可能与您的报价有关
var txt1 = "<button type = "button" class = "btn btn-primary">text</button>";
$("#thing1").append(txt1); // Append new elements
尝试
var txt1 = '<button type = "button" class = "btn btn-primary">text</button>';
$("#thing1").append(txt1); // Append new elements