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

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

getElementById on a Dynamically Created Element That Is NOT a Button

问题

index.js

  1. // 所有内容都附加到这个div中。
  2. const content = document.createElement('div');
  3. content.setAttribute('id', 'content');
  4. function component(focus) {
  5. content.appendChild(nav());
  6. content.appendChild(tabs());
  7. content.appendChild(pageLoad(focus));
  8. return content;
  9. }
  10. document.body.appendChild(component("home"));
  11. let cont = document.getElementById('main-section');
  12. document.getElementById('tab1').addEventListener('click', (e) => {
  13. content.removeChild(cont);
  14. content.appendChild(pageLoad("home"));
  15. });
  16. document.getElementById('tab2').addEventListener('click', (e) => {
  17. content.removeChild(cont);
  18. content.appendChild(pageLoad("about"));
  19. });

pageLoad.js

  1. import home from "./pages/home";
  2. import about from "./pages/about";
  3. export default function pageLoad(focus) {
  4. const content = document.createElement('section');
  5. content.setAttribute('class', 'main');
  6. content.setAttribute('id', 'main-section');
  7. if (focus == "home") {
  8. content.appendChild(home());
  9. } else if (focus == "about") {
  10. content.appendChild(about());
  11. }
  12. return content;
  13. };
英文:

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

  1. import pageLoad from './pageLoad';
  2. import nav from "./headers/nav";
  3. import tabs from "./headers/tabs";
  4. //All content is appended to this div.
  5. const content = document.createElement('div');
  6. content.setAttribute('id', 'content');
  7. function component(focus) {
  8. content.appendChild(nav());
  9. content.appendChild(tabs());
  10. content.appendChild(pageLoad(focus));
  11. return content;
  12. }
  13. document.body.appendChild(component("home"));
  14. let cont = document.getElementById('main-section');
  15. document.getElementById('tab1').addEventListener('click', (e) => {
  16. content.removeChild(cont);
  17. content.appendChild(pageLoad("home"));
  18. });
  19. document.getElementById('tab2').addEventListener('click', (e) => {
  20. content.removeChild(cont);
  21. content.appendChild(pageLoad("about"));
  22. });

pageLoad.js

  1. import home from "./pages/home";
  2. import about from "./pages/about";
  3. export default function pageLoad(focus) {
  4. const content = document.createElement('section');
  5. content.setAttribute('class', 'main');
  6. content.setAttribute('id', 'main-section');
  7. if (focus == "home") {
  8. content.appendChild(home());
  9. }
  10. else if (focus == "about") {
  11. content.appendChild(about());
  12. }
  13. return content;
  14. };

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

  1. document.getElementById('tab1').addEventListener('click', () => {
  2. //select the element every time since it is dynamically re-created on click
  3. let cont = document.getElementById('main-section');
  4. content.removeChild(cont);
  5. content.appendChild(pageLoad("home"));
  6. });
  7. document.getElementById('tab2').addEventListener('click', () => {
  8. let cont = document.getElementById('main-section');
  9. content.removeChild(cont);
  10. content.appendChild(pageLoad("menu"));
  11. });
  12. ```"
  13. <details>
  14. <summary>英文:</summary>
  15. 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.
  16. index.js

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

  1. //select the element every time since it is dynamically re-created on click
  2. let cont = document.getElementById(&#39;main-section&#39;);
  3. content.removeChild(cont);
  4. content.appendChild(pageLoad(&quot;home&quot;));

});

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

  1. let cont = document.getElementById(&#39;main-section&#39;);
  2. content.removeChild(cont);
  3. content.appendChild(pageLoad(&quot;menu&quot;));

});

  1. </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:

确定