亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

style.marginLeft 報錯Cannot read property 'style' of undefined

var marginLeft = parseInt(ball.style.marginLeft,10)?

這行報錯,Cannot read property 'style' of undefined?

是不是style.marginLeft這個寫法有問題呢?


正在回答

5 回答

要把_animate()里面的參數刪了,因為你在這個函數外面以及有個ball了。

1 回復 有任何疑惑可以回復我~
#1

一仁 提問者

確實是的啊,非常感謝!
2016-07-26 回復 有任何疑惑可以回復我~
#2

慕粉1657331796

完美解決了問題!
2017-06-17 回復 有任何疑惑可以回復我~

如果你問的是promise改寫之前的話,是因為你的body還沒加載就加載了js, 所以提示undefined

0 回復 有任何疑惑可以回復我~

var Promise = window.Promise
function promiseAnimate(ball,distance){
? ?return new Promise(function(resolve,reject){
? ? ? ?function _animate(ball,distance,cb){
? ? ? ? ? ?setTimeout(function () {
? ? ? ? ? ? ? ?var marginLeft = parseInt(ball.style.marginLeft,10)

? ? ? ? ? ? ? ?if(marginLeft === distance){
? ? ? ? ? ? ? ? ? ?resolve()
? ? ? ? ? ? ? ?}else{
? ? ? ? ? ? ? ? ? ?if(marginLeft < distance){
? ? ? ? ? ? ? ? ? ? ? ?marginLeft++
? ? ? ? ? ? ? ? ? ?}else{
? ? ? ? ? ? ? ? ? ? ? ?marginLeft--
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ?ball.style.marginLeft = marginLeft + 'px'
? ? ? ? ? ? ? ? ? ?_animate()
? ? ? ? ? ? ? ?}
? ? ? ? ? ?},13)
? ? ? ?}
? ? ? ?_animate()
? ?})
}

0 回復 有任何疑惑可以回復我~

一般通過非id和querySelect獲得的dom元素都是一個數組,要加下標比如ball[0]來調用對象的屬性

0 回復 有任何疑惑可以回復我~
#1

_boom

querySelector() 方法返回文檔中匹配指定 CSS 選擇器的一個元素。 注意: querySelector() 方法僅僅返回匹配指定選擇器的第一個元素。如果你需要返回所有的元素,請使用 querySelectorAll() 方法替代。
2016-07-26 回復 有任何疑惑可以回復我~

你看一下你的ball是不是個數組

0 回復 有任何疑惑可以回復我~
#1

一仁 提問者

不是數組,我對比了下老師的代碼,三個球的class都是ball,函數傳入了ball,距離和回調函數,一樣的,不知是何原因
2016-07-24 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
進擊Node.js基礎(二)
  • 參與學習       76735    人
  • 解答問題       242    個

本教程帶你攻破 Nodejs,讓 JavaScript流暢運行在服務器端

進入課程

style.marginLeft 報錯Cannot read property 'style' of undefined

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號