javascript - JQuery 使用引导样式添加元素

标签 javascript jquery html

我正在尝试使用类似于 ( 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>

最佳答案

我认为你只是遇到了一个问题,你应该混合你的 "和你的 '

试试这个:

var txt1 = "<button type = 'button' class = 'btn btn-primary'>text</button>";    

关于javascript - JQuery 使用引导样式添加元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46573824/

相关文章:

javascript - 有没有更简单的方法来更新复选框价格?

javascript - 将 camunda dmn modeler 集成到 spring mvc 应用程序中与 Angular js 前端

java - 与 JQueryMobile 中基于 REST 的 JSON WebService 的连接

javascript - 如何在 if 语句 jQuery 中设置超时

javascript - 如何使用户选择输入的选项,页面刷新后保持不变

html - CSS:将图像放在另一个图像的背面

javascript - 当用户向下滚动时如何使向下箭头消失?

javascript - 提交处理程序 : function not working

PHP : wp_nav_menu adding icons

css - 如何测试CSS方向: auto as an english speaker