【发布时间】:2017-04-23 04:24:07
【问题描述】:
我是 javascript 新手,我只是想在鼠标悬停时移动链接。
我希望下面的代码能做到这一点。但上面写着Uncaught TypeError: Cannot set property 'left' of undefined。
$('a').mouseover(function(){
yPosition = parseInt(Math.random() * window.screen.availHeight);
xPosition = parseInt(Math.random() * window.screen.availWidth);
$(this).style.left = xPosition + "px";
$(this).style.top = yPosition + "px";
});
【问题讨论】:
-
$(this)是一个 jquery 对象。而style是本机 dom 属性。 -
使用jQuery的
$(this).style.left= xPosition + "px";必须是$(this).css('left', xPosition + "px"); -
@choz 是对的。您需要编写选择器并获取 DOM 元素。那就修改吧。
-
您的问题得到解答了吗?如果是这样,请选择一个答案。
-
从不。采用。解析整数。和。数字。
标签: javascript jquery dom typeerror