我想在用户点击的位置准确添加图片。到目前为止我有这个,但它只是将图像添加到顶部并且只是在那里添加...而不是用户点击的位置
<html>
<head>
<script type="text/javascript">
function stamp(d,e)
{
var i = new Image();
i.src = 'smiley.jpg';
document.getElementById('target').appendChild(i);
//document.getElementById('target').style.left = "100px"; //e.clientX ;
//document.getElementById('target').style.right = "1000px"; //e.clientY;
}
</script>
</head>
<body id="target" onclick="javascript:stamp(this,event);" style="left: 100px">
</body>
</html>
答案 0 :(得分:6)
window.onclick = function(event) {
var i = new Image();
i.src = 'http://jsfiddle.net/img/logo.png';
i.style.position = "absolute";
i.style.left = event.clientX + 'px';
i.style.top = event.clientY + 'px';
event.target.appendChild(i);
}