在一个动态创建的不是按钮的元素上使用 getElementById

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

getElementById on a Dynamically Created Element That Is NOT a Button

问题

index.js

// 所有内容都附加到这个div中。
const content = document.createElement('div');
content.setAttribute('id', 'content');

function component(focus) {
    content.appendChild(nav());
    content.appendChild(tabs());
    content.appendChild(pageLoad(focus));
    return content;
}

document.body.appendChild(component("home"));

let cont = document.getElementById('main-section');

document.getElementById('tab1').addEventListener('click', (e) => {
    content.removeChild(cont);
    content.appendChild(pageLoad("home"));
});

document.getElementById('tab2').addEventListener('click', (e) => {
    content.removeChild(cont);
    content.appendChild(pageLoad("about"));
});

pageLoad.js

import home from "./pages/home";
import about from "./pages/about";

export default function pageLoad(focus) {
    const content = document.createElement('section');
    content.setAttribute('class', 'main');
    content.setAttribute('id', 'main-section');

    if (focus == "home") {
        content.appendChild(home());
    } else if (focus == "about") {
        content.appendChild(about());
    }

    return content;
};
英文:

I have made a page with tabbed browsing. I have the page set to re-populate the page based on which tab is clicked. I do this using JavaScript.

I select the section that is to be re-populated using getElementById. When a tab is clicked, the section is removed using its parent node and re-inserted using JavaScript. However, when trying to click a tab complete the same action, I get an error that states "Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node."

I understand this is due to the element being created dynamically.

After researching I found that event delegation could be a solution. However, for the examples I've found they involve handling button clicks on dynamic elements. I'm trying to select an element that is created as a result of clicking any of the tab buttons.

This is my code

index.js

import pageLoad from './pageLoad';
import nav from "./headers/nav";
import tabs from "./headers/tabs";

//All content is appended to this div.
const content = document.createElement('div');
content.setAttribute('id', 'content');

function component(focus) {

    content.appendChild(nav());
    content.appendChild(tabs());

    content.appendChild(pageLoad(focus));
 
    return content;
}

document.body.appendChild(component("home"));

let cont = document.getElementById('main-section');

document.getElementById('tab1').addEventListener('click', (e) => {

    content.removeChild(cont);
    content.appendChild(pageLoad("home"));
});

document.getElementById('tab2').addEventListener('click', (e) => {

    content.removeChild(cont);
    content.appendChild(pageLoad("about"));
});


pageLoad.js

import home from "./pages/home";
import about from "./pages/about";

export default function pageLoad(focus) {

    const content = document.createElement('section');
    content.setAttribute('class', 'main');
    content.setAttribute('id', 'main-section');

    if (focus == "home") {


        content.appendChild(home());
    }
    
    else if (focus == "about") {

        content.appendChild(about());

    } 
    
    return content;

};

答案1

得分: 0

这是你要翻译的内容:

"Found a solution to this. Simply move the cont variable inside the event listeners so that it is initialized every time the button is clicked. This allows the main-section element to be re-selected each time even though it has been dynamically created.

index.js

document.getElementById('tab1').addEventListener('click', () => {

    //select the element every time since it is dynamically re-created on click
    let cont = document.getElementById('main-section');

    content.removeChild(cont);
    content.appendChild(pageLoad("home"));
});

document.getElementById('tab2').addEventListener('click', () => {

    let cont = document.getElementById('main-section');

    content.removeChild(cont);
    content.appendChild(pageLoad("menu"));
});
```"

<details>
<summary>英文:</summary>

Found a solution to this. Simply move the **cont** variable inside the event listeners so that it is initialized every time the button is clicked. This allows the **main-section** element to be re-selected each time even though it has been dynamically created.

index.js

document.getElementById('tab1').addEventListener('click', () => {

//select the element every time since it is dynamically re-created on click
let cont = document.getElementById(&#39;main-section&#39;);

content.removeChild(cont);
content.appendChild(pageLoad(&quot;home&quot;));

});

document.getElementById('tab2').addEventListener('click', () => {

let cont = document.getElementById(&#39;main-section&#39;);

content.removeChild(cont);
content.appendChild(pageLoad(&quot;menu&quot;));

});


</details>



huangapple
  • 本文由 发表于 2023年6月29日 10:08:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577671.html
匿名

发表评论

匿名网友

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

确定