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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用JS 遍歷 json時遇到的問題。attr.step為什么是undefined。

使用JS 遍歷 json時遇到的問題。attr.step為什么是undefined。

地獄快車_666 2016-08-24 14:28:45
<!DOCTYPE?html> <html?lang="en"> <head> ????<meta?charset="UTF-8"> ????<title>Title</title> ????<script> ?function?getStyle(obj,attr){ ?????if?(obj.currentStyle){ ?????????return?obj.currentStyle[attr]; ?????}else{ ????????return?window.getComputedStyle(obj,null)[attr]; ?????} ?} ?function??animate_arg(obj,json,speed){ ?????for(var?attr?in?json){ ???var?current?=?parseInt(getStyle(obj,attr)); ???console.log(attr); ?????console.log(typeof(attr)); ?????//比如此處傳josn{top:500}進來的時候console.log(attr)就是輸出top,那下面attr.step?json的值傳進來就會生成相應的 ?????//width.step,height.step,left.step?但是我實際我attr.step得到的是個未定義的。 ?????attr.step?=?json[attr]>current???speed?:?-speed; ?????console.log(typeof(attr.step)); ????? ?????attr.timer?=?setInterval(function(){ ???var?current?=?parseInt(getStyle(obj,attr)); ?????var?result?=?Math.abs(json[attr]?-?current); ?????????if?(result?<=?speed){ ?????????obj.style[attr]=json[attr]; ?????????}else{ ?????????obj.style[attr]=?current?+?attr.step?+?"px"; ?????????} ????},30) ???} ??} ???window.onload=function(){ ?????var?btn200?=?document.getElementById("btn200"); ?????var?btn400?=?document.getElementById("btn400"); ?????var?box?=?document.getElementById("box"); ?????btn200.onclick?=?function()?{ ?????????animate_arg(box,{width:?200,?top:?800,left:?200},10); ?????}; ?????btn400.onclick?=?function()?{ ????????animate_arg(box,{top:500},10); ??????} ???} ????</script> ????<style> ?????????div?{ ?????????????width:?100px; ?????????????height:?100px; ?????????????background-color:?pink; ?????????????position:?absolute; ?????????????left:?0; ?????????????top:?50px; ?????????????border-radius:?50%; ?????????} ????</style> </head> <body> ????<button?id="btn200">200</button> ????<button?id="btn400">400</button> ????<div?id="box"></div> </body> </html>
查看完整描述

2 回答

?
地獄快車_666

TA貢獻4條經驗 獲得超0個贊

原因在于attr不是對象或數組不能添加屬性

查看完整回答
反對 回復 2016-08-26
?
qq_sU_4

TA貢獻30條經驗 獲得超6個贊

為什么要attr.step,直接var 個 step不可以嗎

查看完整回答
反對 回復 2016-08-25
  • 地獄快車_666
    地獄快車_666
    json會傳多個值進來,可能是width,left,height等等,你直接用step不能區分,你下面函數里for循環的時候用width.step,left.step才能區分吧。
  • 2 回答
  • 0 關注
  • 1436 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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