我有一张图片。在这里,我编写了如下所示的jQuery:
jQuery('.image').click(function(){
var path = $(this).attr('src');
});
现在,我想为此路径触发复制事件。当我按下ctrl + v按钮然后它打印在路径上面所以我应该写什么jQuery?
我需要自动解雇复制事件。那么有可能吗?
答案 0 :(得分:1)
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'));
});
答案 1 :(得分:0)
我想你想要这个
<script>
$(document).ready(function(){
var path;
$(document).on('click','.image',function()
{
path = $(this).attr('src');
});
$(document).on('keydown',function(evt)
{
if(evt.ctrlKey && evt.which == 86)
{
$('#field').html(path);
}
});
});
</script>
<body>
<img src="sample/welcome/thing.jpg" alt="no image" class="image"/>
<p id="field"></p>
</body>