【发布时间】:2023-04-07 07:37:01
【问题描述】:
我已经有一个禁用长按的可用代码 但现在我不想通过 ID 获取元素。我不可能为每个特定项目添加 Id。 我想让它适用于名称标签中的每个 img。 但是,它现在不起作用。请帮忙。
原工作线: preventLongPressMenu(document.getElementById('theimage'));
编辑的行: preventLongPressMenu(document.getElementByTagName('body img'));
整个代码:
<!DOCTYPE html>
<html>
<head>
<script>
function absorbEvent_(event) {
var e = event || window.event;
e.preventDefault && e.preventDefault();
e.stopPropagation && e.stopPropagation();
e.cancelBubble = true;
e.returnValue = false;
return false;
}
function preventLongPressMenu(node) {
node.ontouchstart = absorbEvent_;
node.ontouchmove = absorbEvent_;
node.ontouchend = absorbEvent_;
node.ontouchcancel = absorbEvent_;
}
function init() {
preventLongPressMenu(document.getElementByTagName('body img'));
}
</script>
<style>
*:not(input):not(textarea) {
-webkit-user-select: none; /* disable selection/Copy of UIWebView */
-webkit-touch-callout: none; /* disable the IOS popup when long-press on a link */
}
</style>
</head>
<body onload="init()" id="theimage" >
<img src="http://www.google.com/logos/arthurboyd2010-hp.jpg" width="400">
</body>
</html>
【问题讨论】:
标签: javascript html long-press