![](/img/trans.png)
[英]Why am I getting this error: Uncaught TypeError: cannot read property 'john' of undefined
[英]Why am I getting the error: Uncaught TypeError: Cannot read property 'left' of undefined
HTML:
<div id="opening_0" style="background-color: #bfbfbf; position: absolute; left: 45px; top: 45px; height: 202.5px; width: 157.5px; overflow: hidden; z-index: 4;" ondrop="drop(event, this)" ondragover="allowDrop(event)" onclick="photos_add_selected_fid(this);"> </div>
使用Javascript:
canvas.style.left = $("#opening_0").style.left - img.width + "px";
canvas.style.top = $("#opening_0").style.top - img.height + "px";
为什么会出现此错误”?
使用jQuery的.css()函数,因为这基本上就是您使用jQuery的原因:
canvas.style.left = parseInt($("#opening_0").css('left')) - img.width + "px";
该错误是因为$("#opening_0")
返回包装了匹配的DOM节点的jQuery对象。 要访问底层的DOM节点,可以将其视为数组:例如, $("#opening_0")[0]
将起作用。
但是 ,此方法效果不好,因为style.left
将返回可能具有px或百分比值的字符串。 您可以使用$("#opening_0").offset().left
代替,它将始终返回数字值。
jQuery对象没有style
属性。
如果您想访问它,请使用香草javascript方法:
document.getElementById("opening_0").style.left;
...或“解开” jQuery对象,如下所示:
$("#opening_0")[0].style.left;
我建议使用第一种方法。 只需记住使用parseInt()
解析值即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.