javascript - 复制图片路径点击即可

标签 javascript jquery

我有一张图片。在这里,我编写了如下所示的 jQuery:

jQuery('.image').click(function(){
    var path = $(this).attr('src');
});

现在,我想触发此路径的复制事件。当我按下 ctrl+v 按钮时,它会打印在路径上方,所以我应该编写什么 jQuery?

我需要自动触发复制事件。那么有可能吗?

最佳答案

Link

html

<img class="image" src="http://pic.1fotonin.com/data/wallpapers/93/WDF_1369581.jpg" />  
<img class="image" src="https://c7.staticflickr.com/3/2538/3742771246_2648fa4e6e_b.jpg" />

CSS

.image{
width:200px;
}

JavaScript

$('.image').click(function(){
// Create an auxiliary hidden input
 var aux = document.createElement("input");

// Get the text from the element passed into the input
aux.setAttribute("value", $(this).attr('src'));

// Append the aux input to the body
document.body.appendChild(aux);

// Highlight the content
aux.select();

// Execute the copy command
document.execCommand("copy");

// Remove the input from the body
document.body.removeChild(aux);
 alert($(this).attr('src')); 
});

关于javascript - 复制图片路径点击即可,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37300695/

相关文章:

javascript - 如何将 css 文件添加到打开的窗口

javascript - 使用 passport.js、sails.js 和 sails 生成身份验证保护页面

javascript - 当浏览器窗口完全加载时,使用 fadeOut() 或 hide() 淡出/隐藏 Bootstrap 4 spinner/loader

javascript - 使用内部 anchor 将类添加到事件菜单项

JavaScript – 检测元素是否在视口(viewport)中停留 n 秒

jquery - ajax函数成功后替换div中的内容

javascript - jQuery UI 对话框 : How check what if user confirm or cancel the UI dialog.

javascript - 重写 jQuery 事件处理程序

javascript - 不返回任何内容的 web 服务的推荐返回类型(使用 jquery 调用)

javascript - 我的 React 组件如何理解 'children' 属性?