为什么使用JS将HTML元素添加到DOM时没有Bootstrap属性?

huangapple go评论67阅读模式
英文:

Why don't HTML elements added to the DOM using JS have Bootstrap properties?

问题

I am working on a project which uses bootstrap to create various nested components which are styled according to Bootstrap's class system. I have already created what I deem a successful card component using bootstrap, but when trying to clone this element using the exact same classes and layout using JS and the DOM system, the appended card component has HTML default styles rather than bootstrap's styles. The classes and nesting are exactly the same.

I tried combing through my code line by line and making sure that the layout was an exact duplicate. Here is an example of my base HTML code not produced by JS:

<div class="row bookContainer">
    <div class="col-md-4">
        <div class="card">
            <div class="card-body">
                <div class="card-title">Title Here</div>
                <div class="card-text">
                    <p>By Mr. Ipsum Dolor</p>
                    <p>Pages: 2000</p>
                    <button type="button" class="btn btn-outline-success  complete">Completed</button>
                    <button type="button" class="btn btn-outline-danger">Remove</button>
                </div>
            </div>
        </div>
    </div>
</div>

and an accompanying image of the card it produced:

Successful component.

I then used the following JS code to produce a clone of the element layout and add it to the DOM:

Step 1: Converting book object to HTML

function bookToCard(book) {
    // col div
    const colDiv = createDiv();
    colDiv.classList.add("col-md-4");

    // card div
    const card = createDiv()
    card.classList.add("card");

    // card body
    const cardBody = createDiv();
    cardBody.classList.add("card-body");

    // card title
    const cardTitle = createDiv();
    cardTitle.classList.add("card-title");

    // adding title content
    let titleContent = createText(book.title);
    cardTitle.appendChild(titleContent);
    cardBody.appendChild(cardTitle);

    // card text and content
    const cardText = createDiv();
    cardText.classList.add("card-text");

    let authorParagraph = document.createElement("p");
    authorParagraph.appendChild(createText(book.author));
    cardText.appendChild(authorParagraph);

    let numParagraph = document.createElement("p");
    numParagraph.appendChild(createText(book.pages));
    cardText.appendChild(numParagraph);

    // buttons
    let cButton = document.createElement("button"); // complete or incomplete button
    cButton.classList.add("btn");
    if (book.completed) {
        cButton.classList.add("btn-outline-success");
    } else {
        cButton.classList.add("btn-outline-secondary");
    }
    cardText.appendChild(cButton);

    let rButton = document.createElement("button");
    rButton.classList.add("btn", "btn-outline-danger");
    cardText.appendChild(rButton);

    cardBody.appendChild(cardText);
    card.appendChild(cardBody);
    colDiv.appendChild(card);

    return colDiv;
}

Step 2: Updating HTML display

function updateDisplay() {
    let container = document.querySelector(".bookContainer");
    container.innerHTML = '';
    container.classList.add("row", "bookContainer");
    for (let i = 0; i < library.length; i++) {
        container.appendChild(bookToCard(library[i]));
    }
}

The process results in the following element being added to the page (with the successful version shown at left):

enter image description here.

Any help on how to get the right card to have Bootstrap properties would be appreciated. Sorry for the sloppy code.

英文:

I am working on a project which uses bootstrap to create various nested components which are styled according to Bootstrap's class system. I have already created what I deem a successful card component using bootstrap, but when trying to clone this element using the exact same classes and layout using JS and the DOM system, the appended card component has HTML default styles rather than bootstrap's styles. The classes and nesting are exactly the same.

I tried combing through my code line by line and making sure that the layout was an exact duplicate. Here is an example of my base HTML code not produced by JS:

&lt;div class=&quot;row bookContainer&quot;&gt;
&lt;div class=&quot;col-md-4&quot;&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;div class=&quot;card-body&quot;&gt;
&lt;div class=&quot;card-title&quot;&gt;Title Here&lt;/div&gt;
&lt;div class=&quot;card-text&quot;&gt;
&lt;p&gt;By Mr. Ipsum Dolor&lt;/p&gt;
&lt;p&gt;Pages: 2000&lt;/p&gt;
&lt;button type=&quot;button&quot; class=&quot;btn btn-outline-success  complete&quot;&gt;Completed&lt;/button&gt;
&lt;button type=&quot;button&quot; class=&quot;btn btn-outline-danger&quot;&gt;Remove&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

and an accompanying image of the card it produced:

Successful component.

I then used the following JS code to produce the a clone of the element layout and add it to the DOM:

Step 1: Converting book object to html

function bookToCard(book) {
//col div
const colDiv = createDiv();
colDiv.classList.add(&quot;col-md-4&quot;);
//card div
const card = createDiv()
card.classList.add(&quot;card&quot;);
//card body
const cardBody = createDiv();
cardBody.classList.add(&quot;card-body&quot;);
//card title
const cardTitle = createDiv();
cardTitle.classList.add(&quot;card-title&quot;);
//adding title content
let titleContent = createText(book.title);
cardTitle.appendChild(titleContent);
cardBody.appendChild(cardTitle);
//card text and content
const cardText = createDiv();
cardText.classList.add(&quot;card-text&quot;);
let authorParagraph = document.createElement(&quot;p&quot;);
authorParagraph.appendChild(createText(book.author));
cardText.appendChild(authorParagraph);
let numParagraph = document.createElement(&quot;p&quot;);
numParagraph.appendChild(createText(book.pages));
cardText.appendChild(numParagraph);
//buttons
let cButton = document.createElement(&quot;button&quot;); //complete or incomplete button
cButton.classList.add(&quot;btn&quot;);
if (book.completed) {
cButton.classList.add(&quot;btn-outline-success&quot;);
} else {
cButton.classList.add(&quot;btn-outline-secondary&quot;);
}
cardText.appendChild(cButton);
let rButton = document.createElement(&quot;button&quot;);
rButton.classList.add(&quot;btn&quot;, &quot;btn-outline-danger&quot;);
cardText.appendChild(rButton);
cardBody.appendChild(cardText);
card.appendChild(cardBody);
colDiv.appendChild(card);
return colDiv;
}

Step 2: updating HTML display

function updateDisplay() {
let container = document.querySelector(&quot;.bookContainer&quot;);
container.innerHTML = &#39;&#39;;
container.classList.add(&quot;row&quot;, &quot;bookContainer&quot;);
for (let i = 0; i &lt; library.length; i++) {
container.appendChild(bookToCard(library[i]));
}
}

The process results in the following element being added to the page (with the successful version shown at left):

enter image description here.

Any help on how to get the right card to have Bootstrap properties would be appreciated. Sorry for the sloppy code.

答案1

得分: 1

基于这些图片,问题不在于样式,因为Bootstrap类已应用于新卡片。问题在于您忘记为创建的 <button><div> 添加文本。

对于 cButton

let cButton = document.createElement("button"); 
cButton.classList.add("btn");
if (book.completed) {
    cButton.classList.add("btn-outline-success");
    cButton.textContent = "已完成"; // <<< 这里
} else {
    cButton.classList.add("btn-outline-secondary");
    cButton.textContent = "进行中"; // <<< 这里
}

对于 author

let authorParagraph = document.createElement("p");
authorParagraph.textContent = "作者:" + createText(book.author); // <<< 添加了 "作者:"

需要对 pagesRemove 做相同的操作。

英文:

Based on the images, the issue is not the styling, as bootstrap classes have been applied to the new card. The issue is you forgetting to add text to created &lt;button&gt;s and &lt;div&gt;s.

For cButton

let cButton = document.createElement(&quot;button&quot;); 
cButton.classList.add(&quot;btn&quot;);
if (book.completed) {
cButton.classList.add(&quot;btn-outline-success&quot;);
cButton.textContent = &quot;Completed&quot;; // &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; here
} else {
cButton.classList.add(&quot;btn-outline-secondary&quot;);
cButton.textContent = &quot;In Progress&quot;; // &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; and here
}

And for author

let authorParagraph = document.createElement(&quot;p&quot;);
authorParagraph.appendChild(&quot;By &quot; + createText(book.author));  // &lt;&lt;&lt;&lt;&lt; added &quot;By &quot;

Need to do the same for pages and Remove

huangapple
  • 本文由 发表于 2023年6月1日 06:11:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377608.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定