简体   繁体   中英

JQuery add elements with bootstrap styling

I am trying to add a button to a specific HTML element using jQuery similar to, ( https://www.w3schools.com/jquery/jquery_dom_add.asp )

My issue is that when I try and run the code,

var txt1 = "<button type = "button" class = "btn btn-primary">text</button>";        
$("#thing1").append(txt1);     // Append new elements

I don't get the styling I am wanting and it is messing up another part of my code unless I do the same as above, except without the bootstrap styling like,

    var txt1 = "<button">text</button>";        
    $("#thing1").append(txt1);     // Append new elements

It works fine with the second example except for that the button styling doesn't use bootstrap like I am wanting in the first example and am not sure as to why?

A more complete picture of the code is below,

    <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>

I think you just have a problem where you should be mixing your " and your '

try this:

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

Maybe the problem is with your quotes

var txt1 = "<button type = "button" class = "btn btn-primary">text</button>";        
$("#thing1").append(txt1);     // Append new elements

try

var txt1 = '<button type = "button" class = "btn btn-primary">text</button>';        
$("#thing1").append(txt1);     // Append new elements

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM