如何在Google Earth Pro的Placemark中实现剪贴板复制按钮?
解决Google Earth Pro Placemark中复制按钮失效的问题
Google Earth Pro的内置HTML渲染环境不支持现代的navigator.clipboardAPI,这是导致你原有代码点击无响应的核心原因。可以改用兼容性更好的document.execCommand('copy')方法实现复制功能,同时调整HTML结构适配Placemark的限制:
可行代码示例
<input type="text" value="House number is 257641" id="myInput" style="width:100%;"> <button onclick="myFunction()">Copy text</button> <script> function myFunction() { var copyText = document.getElementById("myInput"); // 选中输入框内容 copyText.select(); // 兼容旧环境的复制命令 document.execCommand('copy'); // 取消选中状态(可选优化) window.getSelection().removeAllRanges(); // 提示复制成功(Google Earth Pro支持alert弹窗) alert("Copied the text: " + copyText.value); } </script>
使用注意事项
- 直接将上述代码粘贴到Placemark的描述框中即可,不需要保留外层的
<!DOCTYPE html>、<html>、<body>标签 - 若不想显示输入框,可给input添加样式
style="position:absolute;left:-9999px;"将其隐藏,复制逻辑不受影响 - 部分旧版本Google Earth Pro可能限制脚本执行,建议升级到最新版本测试
内容的提问来源于stack exchange,提问作者Raffa
相关产品推荐
相关产品推荐

