繁体   English   中英

删除元素数组上的事件侦听器

[英]Remove Event Listeners on Array of Elements

一旦特定的 class 添加到父元素,我正在尝试删除元素组上的事件侦听器。 一旦我在某个元素上单击启用编辑和 hover,该元素就会被选中。 单击禁用编辑按钮后,我希望能够禁用选择元素。 这是小提琴https://jsfiddle.net/chille1987/a1d3gb7u/15/

HTML

<button class="enable-editing">Enable Editing</button>
<button class="disable-editing">Disable Editing</button>

<div id="editor">
    <p>This is first paragraph</p>
    <p>This is second paragraph</p>
    <h1>This is heading 1</h1>
    <p class="empty-paragraph"><br /></p>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    <h2>This is heading 2</h2>
    <p>Another paragraph goes here</p>
    <h3>H3 heading goes here</h3>
</div>

JAVASCRIPT

const enable = document.querySelector('.enable-editing');
const disable = document.querySelector('.disable-editing');
const editor = document.querySelector('#editor');
let target;

enable.addEventListener('click', () => {
    editor.classList.add('editing-enabled')
    let allElements = Array.from(document.querySelectorAll('.editing-enabled *'));
    allElements.forEach(child => {
        child.addEventListener('mouseenter', (e) => {
            e.preventDefault();
            target = e.target;
            selectText(target);
        })
    })
})

disable.addEventListener('click', () => {
    editor.classList.remove('editing-enabled')
})

function selectText(node) {
    node = node;
    if (document.body.createTextRange) {
        const range = document.body.createTextRange();
        range.moveToElementText(node);
        range.select();
    } else if (window.getSelection) {
        const selection = window.getSelection();
        const range = document.createRange();
        range.selectNodeContents(node);
        selection.removeAllRanges();
        selection.addRange(range);
    } else {
        console.warn("Could not select text in node: Unsupported browser.");
    }
}

我有一些变化:

 const enable = document.querySelector('.enable-editing'); const disable = document.querySelector('.disable-editing'); const editor = document.querySelector('#editor'); let target; const listener = (e) => { e.preventDefault(); target = e.target; selectText(target); } enable.addEventListener('click', () => { editor.classList.add('editing-enabled') let allElements = Array.from(document.querySelectorAll('.editing-enabled *')); allElements.forEach(child => { child.addEventListener('mouseenter', listener) }) }) disable.addEventListener('click', () => { let allElements = Array.from(document.querySelectorAll('.editing-enabled *')); allElements.forEach(child => { child.removeEventListener('mouseenter', listener, { passive: true }) }) editor.classList.remove('editing-enabled') }) function selectText(node) { node = node; if (document.body.createTextRange) { const range = document.body.createTextRange(); range.moveToElementText(node); range.select(); } else if (window.getSelection) { const selection = window.getSelection(); const range = document.createRange(); range.selectNodeContents(node); selection.removeAllRanges(); selection.addRange(range); } else { console.warn("Could not select text in node: Unsupported browser."); } }
 <button class="enable-editing">Enable Editing</button> <button class="disable-editing">Disable Editing</button> <div id="editor"> <p>This is first paragraph</p> <p>This is second paragraph</p> <h1>This is heading 1</h1> <p class="empty-paragraph"><br /></p> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <h2>This is heading 2</h2> <p>Another paragraph goes here</p> <h3>H3 heading goes here</h3> </div>

您可以在此处使用更多选项: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener

暂无
暂无

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

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