Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

有更简单更好的实现 #2

Closed
ShenHongFei opened this issue Mar 18, 2024 · 4 comments · Fixed by #3
Closed

有更简单更好的实现 #2

ShenHongFei opened this issue Mar 18, 2024 · 4 comments · Fixed by #3
Assignees

Comments

@ShenHongFei
Copy link
Contributor

document.querySelectorAll<HTMLDivElement>('#article-content').forEach($div => {
    $div.addEventListener(
        'copy', 
        event => { event.stopImmediatePropagation() }, 
        true
    )
})
@Mehver
Copy link
Member

Mehver commented Apr 5, 2024

学习了!大佬愿意给个PR吗 @ShenHongFei ?谢谢!

@Mehver Mehver self-assigned this Apr 5, 2024
Mehver added a commit that referenced this issue Jul 12, 2024
Co-authored-by: ShenHongFei <shen.hongfei@outlook.com>
@Mehver Mehver mentioned this issue Jul 12, 2024
@Mehver Mehver linked a pull request Jul 12, 2024 that will close this issue
@Mehver Mehver closed this as completed in #3 Jul 12, 2024
@Mehver
Copy link
Member

Mehver commented Jul 12, 2024

大大 @ShenHongFei 您不理我,我用 GitHub 的 Co-authored-by 功能直接把您 sign 到参考了您代码的 commit 上了。

另外想请教一下,我记得几个月前试您的代码时能直接用,但是今天想起来再试的时候,我这个新装的 Chrome 下,就报错了。

image

GPT 说是语法兼容性问题,所以后续我也只是参考了您用的 stopImmediatePropagation()true。我对 JS 和前端的理解都不深,我这种写法的兼容性怎么样?

(function () {
'use strict';
// 此实现参考自 Issue #2 中 @ShenHongFei 的代码
document.querySelector('body').addEventListener(
'copy',
event => {
event.stopImmediatePropagation();
},
true
);
})();

@ShenHongFei
Copy link
Contributor Author

好的没问题的,代码直接用就行,在控制台里执行的时候把 <HTMLDivElement> 这个 typescript 类型注解去掉,就不会报错了

@Mehver
Copy link
Member

Mehver commented Jul 12, 2024

哦哦,原来是 typescript 的类型注释😂,学习了!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants