如何将图像id传递到javascript页面
我想将动态图像的id传递给javascript函数,但总是得到null或[ObjectHtmleElement] 这是我的密码:如何将图像id传递到javascript页面,javascript,php,Javascript,Php,我想将动态图像的id传递给javascript函数,但总是得到null或[ObjectHtmleElement] 这是我的密码: function image_select(id) { document.onmousemove=function(e) { var avatar = document.getElementsById(id); avatar.style.position='relative'; avatar.style.le
function image_select(id) {
document.onmousemove=function(e) {
var avatar = document.getElementsById(id);
avatar.style.position='relative';
avatar.style.left = Math.min(Math.max(e.x-530,-25 ),25)+ "px";
avatar.style.top = Math.max(Math.min(e.y-256,90),-8) + "px";
}
}
function onup(id) {
document.onmouseup=function() {
document.getElementById(id).style.position='relative';
document.onmousemove=null
}
}
<div id="<?php echo $line4->id."back"; ?>" style="background-image:url(images/fp1.jpg);background-size:100%; padding:0px; margin:0px; background-repeat:no-repeat; border:thin solid black; width:130px; height:130px;" >
<img name="<?php echo $line->id ?>" src="<?php echo "admin/files/".$line4->image_path3; ?>"
style="z-index:1; opacity:.4;"
onMouseDown="image_select(<?php echo $line->id ?>);"
onMouseUp="onup(<?php echo $line->id ?>);" />
</div>
函数图像_选择(id){
document.onmousemove=函数(e){
var avatar=document.getElementsById(id);
avatar.style.position='relative';
avatar.style.left=Math.min(Math.max(e.x-530,-25),25)+“px”;
avatar.style.top=Math.max(Math.min(e.y-256,90),-8)+“px”;
}
}
功能onup(id){
document.onmouseup=function(){
document.getElementById(id).style.position='relative';
document.onmousemove=null
}
}
您确定:
var avatar = document.getElementsByName(id);
不应该是:
var avatar = document.getElementById(id);
getElementsByName
方法将返回元素的HTMLCollection,您可以这样访问:
avatar[0].textContent = "Yay, I'm the first matched item";
您真正需要的是:
onMouseDown="image_select(this);"
及
使用
document.getElementById(id)
而不是
document.getElementsByName(id)
链接:$line4->id
我不认为这个id来自javascript,您不能将javascript内容传递给PHP当您实际为图像提供id时会发生什么情况?我不明白您想要什么?当我将图像的动态id传递给javascript函数时,它显示为零或空-正确的来源:事实上这是错误的…我也尝试传递名称,但得到空或空会影响其工作..但问题不大对于MouseMove上的各种图像来说,这是不协调的…这与图像无关。我应该做些什么来纠正它?如果您不想在多个项目周围拖动,您将需要更复杂的内容,因为这可能只会拖动您单击的一个项目。
document.getElementsByName(id)