我有一张图片。在这里,我编写了如下所示的 jQuery:
jQuery('.image').click(function(){
var path = $(this).attr('src');
});
现在,我想触发此路径的复制事件。当我按下 ctrl+v 按钮时,它会打印在路径上方,所以我应该编写什么 jQuery?
我需要自动触发复制事件。那么有可能吗?
最佳答案
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/