最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use removeEventListener for clipboardData? - Stack Overflow

programmeradmin6浏览0评论

Page had the button which copies text to clipboard with code:

export class ClipboardService {
    static copyToClipboard(toCopy: string) : void {
        document.addEventListener('copy', (e : ClipboardEvent) => {
            e.clipboardData.setData('text/plain', toCopy);
            e.preventDefault();
        });
        document.execCommand('copy');
    }
} 

But after at use, this code Ctrl+C doesn't work. I need removeEventListener something like:

export class ClipboardService {
    static copyToClipboard(toCopy: string) : void {
        document.addEventListener('copy', (e : ClipboardEvent) => {
            e.clipboardData.setData('text/plain', toCopy);
            e.preventDefault();
        });
        document.execCommand('copy');

        document.removeEventListener('copy', (e : ClipboardEvent) => {
            e.clipboardData.??? // I stuck in this place.
        });
    }
}

How to go back to standard behavior for clipboard when text from a specific field copied?

Page had the button which copies text to clipboard with code:

export class ClipboardService {
    static copyToClipboard(toCopy: string) : void {
        document.addEventListener('copy', (e : ClipboardEvent) => {
            e.clipboardData.setData('text/plain', toCopy);
            e.preventDefault();
        });
        document.execCommand('copy');
    }
} 

But after at use, this code Ctrl+C doesn't work. I need removeEventListener something like:

export class ClipboardService {
    static copyToClipboard(toCopy: string) : void {
        document.addEventListener('copy', (e : ClipboardEvent) => {
            e.clipboardData.setData('text/plain', toCopy);
            e.preventDefault();
        });
        document.execCommand('copy');

        document.removeEventListener('copy', (e : ClipboardEvent) => {
            e.clipboardData.??? // I stuck in this place.
        });
    }
}

How to go back to standard behavior for clipboard when text from a specific field copied?

Share Improve this question asked Aug 14, 2018 at 14:20 PavelPavel 2,1016 gold badges44 silver badges75 bronze badges 3
  • Possible duplicate of event.clipboardData.setData in copy event – Feras Al Sous Commented Aug 14, 2018 at 14:22
  • 3 To remove the event listener, save the handler as a seperate function. You have to use the same function in both addEventListener and removeEventListener. const doCopy = (e: .... ) => ... and .addEventListener( 'copy', doCopy ); and .removeEventListener( 'copy', doCopy ); Two functions, even if they have exactly the same code inside, are considered different functions. – Shilly Commented Aug 14, 2018 at 14:27
  • @Shilly yes this work, thank You. Plese add your ment as an answer for the adoption. – Pavel Commented Aug 14, 2018 at 14:35
Add a ment  | 

1 Answer 1

Reset to default 12

The problem is that you're trying to remove the function by writing it again. In JS, two function, even with exactly the same code, are still different functions.

So you need to have a reference to the function to be able to use it in both calls:

export class ClipboardService {
static copyToClipboard(toCopy: string) : void {
    const create_copy = (e : ClipboardEvent) => {
        e.clipboardData.setData('text/plain', toCopy);
        e.preventDefault();
    };
    document.addEventListener('copy', create_copy );
    document.execCommand('copy');
    document.removeEventListener('copy', create_copy );
}

}

发布评论

评论列表(0)

  1. 暂无评论