Javascript:getValueProperty是否为null?
我试图编写单个函数来移动元素,但遇到以下错误: 未捕获的TypeError:无法调用null的方法“getPropertyValue”Javascript:getValueProperty是否为null?,javascript,css,animation,element,Javascript,Css,Animation,Element,我试图编写单个函数来移动元素,但遇到以下错误: 未捕获的TypeError:无法调用null的方法“getPropertyValue” function Mover() { this.move = move; // Name of the moving element, property to change, endPoint - where to end the movement function move(element, property, endPoi
function Mover() {
this.move = move;
// Name of the moving element, property to change, endPoint - where to end the movement
function move(element, property, endPoint) {
var element = document.getElementById(element);
// Get value of the desired property and turn it into number so that it can be incremented
var propertyValue = window.getComputedStyle(element).getPropertyValue(property);
var propertyValueInt = parseInt( propertyValue.substr(0,propertyValue.length-2) );
// This is where error starts: for some reason it says the aforementioned error
var moveit = function() {
propertyValueInt = propertyValueInt + 1;
element.style.property = propertyValueInt + "px";
};
window.setInterval(moveit, 500);
}
}
错误可能在哪里?我意识到它以某种方式重写了元素属性,使其为空,但为什么呢
谢谢错误来自
getComputedStyle(元素).getPropertyValue(属性)
,因为元素
不能被识别为HTML元素。您必须修正元素的值
,以引用现有的HTMLElement。函数调用中存在输入错误/错误的文本,或者在执行此函数时,具有所述id
的元素不存在
moveit()
中还有一个错误。当property
是一个变量名时,这并不能满足您的期望:
element.style.property = ...
您需要使用括号表示法:
element.style[property] = ...
从何处将这些
元素、属性、端点
传递到移动
函数?看起来您还需要关于;)的信息。