![](/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.