简体   繁体   English

为什么任何事件在动态加载的代码中都不起作用?

[英]Why doesn't any event work in dynamically loaded code?

I load an HTML page inside a modal我在模态中加载了 HTML 页面

When I put the code out of the modal, the event works ( click, input, and others )当我将代码从模式中取出时,事件会起作用(单击、输入和其他)

NOTE: I don't use and I don't want to use JQuery ( never )注意:我不使用也不想使用 JQuery(从不)

Demo: https://codepen.io/jonathan_silva/pen/vYKqrvE?editors=0010演示: https://codepen.io/jonathan_silva/pen/vYKqrvE?editors=0010

I've been trying to make it work for 3 days.我一直在努力让它工作 3 天。 It looks like everything is fine but...看起来一切都很好,但是...

Help me帮我

const codeHTML = () => {
    const code = `
        <div id="page">
            <div class="steps">
                <article class="step1">
                    <div class="options-grid">
                        <div class="select-box">
                            <div class="options business-options">
                                <div class="business-option">
                                    <input type="radio" class="radio" />
                                    <label>Business A</label>
                                </div>

                                <div class="business-option">
                                    <input type="radio" class="radio" />
                                    <label>Business B</label>
                                </div>

                                <div class="business-option">
                                    <input type="radio" class="radio" />
                                    <label>Business C</label>
                                </div>
                            </div>

                            <div class="select-business">Select Business</div>
                        </div>
                    </div>
                </article>
            </div>
        </div>
    `;

    return code;
}

/* MODAL */
const modal = async ({ target }) => {  
    const html = codeHTML(); // Load HTML page

    let section = document.getElementById('modal-page');

    if (!section) {
        return;
    }

    document.getElementById('modal-content').insertAdjacentHTML('afterbegin', html);

    section.classList.add('modal');

    const onSectionClick = ({ target }) => {
        if (!target.classList.contains('modal-close')) {
            return;
        }

        section.classList.remove('modal');

        section.removeEventListener('click', onSectionClick);

        document.querySelector('#page').remove();
    }

    section.addEventListener('click', onSectionClick);
}

const openModal = document.querySelector('.announce a');
openModal.addEventListener('click', event => modal(event));

/* SELECT */
const selectBusiness = document.querySelector('.select-business');
const businessContainer = document.querySelector('.business-options');
const optionsBusiness = document.querySelectorAll('.business-option');

if (selectBusiness !== null) {
  selectBusiness.addEventListener('click', e => {
    console.log(e); // Nothing happens
    businessContainer.classList.toggle("active");
  });
}

The elements you're trying to querySelector aren't there when you call querySelector , since you only add them to the DOM within the modal() function.当您调用querySelector querySelector ,您尝试查询选择器的元素不存在,因为您只将它们添加到modal() function 中的 DOM。 (You can do eg console.log(selectBusiness) to see that with your own eyes.) (您可以使用console.log(selectBusiness)来亲眼看到这一点。)

You'll need to move that event binding in there, after the insertAdjacentHTML call.insertAdjacentHTML调用之后,您需要将该事件绑定移到那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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