let oBi = document.getElementsByClassName("bi") for (let n = 0; n < oBi.length; i++){ oBi[n].classList.add("box-backhome"); }为什么没有让伪数组对象中的三个元素都被赋值
时间: 2024-03-20 15:42:05 浏览: 44
这段代码错误在于 for 循环中的计数器变量是 `n`,而在循环体内使用了 `i`,导致没有正确遍历伪数组中的元素。应该将循环体中的 `i` 改为 `n`,代码应该如下所示:
```
let oBi = document.getElementsByClassName("bi");
for (let n = 0; n < oBi.length; n++) {
oBi[n].classList.add("box-backhome");
}
```
这样就能正确地遍历伪数组对象中的所有元素,并将其添加 `box-backhome` 类。
相关问题
要想以下JavaScript代码符合预期,空格处应该填什么? var obi = { name: 'xiaoming', hobby: ["basketball','football'],wangwu']friends: ['zhangsan','zhaosi',getNewHobby: function (hobby) this .hobby.push(hobby) getNewFirend: function (firend) [this.firends.push(firend) var a = obi a.friends .push( 'bob') obi.getNewHobby( mountaineering')console.log(obi.friendsl7)预期输出结果: bob
空格处应该填入一个点号 (.)。正确的代码应该是:
```javascript
var obi = {
name: 'xiaoming',
hobby: ['basketball', 'football'],
friends: ['zhangsan', 'zhaosi'],
getNewHobby: function (hobby) {
this.hobby.push(hobby);
},
getNewFriend: function (friend) {
this.friends.push(friend);
}
};
var a = obi;
a.friends.push('bob');
obi.getNewHobby('mountaineering');
console.log(obi.friends[7]);
```
在这段代码中,点号用于访问对象的属性和方法。正确的语法是 `a.friends.push('bob')` 和 `obi.getNewHobby('mountaineering')`。另外,`console.log(obi.friends[7])` 用于输出 `obi` 对象的 `friends` 属性的第 7 个元素,即 'bob'。
private void updateShowSeconds() { if (mShowSeconds) { // Wait until we have a display to start trying to show seconds. if (mSecondsHandler == null && getDisplay() != null) { mSecondsHandler = new Handler(); if (getDisplay().getState() == Display.STATE_ON) { mSecondsHandler.postAtTime(mSecondTick, SystemClock.uptimeMillis() / 1000 * 1000 + 1000); } IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_SCREEN_ON); mContext.registerReceiver(mScreenReceiver, filter); } } else { if (mSecondsHandler != null) { mContext.unregisterReceiver(mScreenReceiver); mSecondsHandler.removeCallbacks(mSecondTick); mSecondsHandler = null; updateClock(); } } } private final BroadcastReceiver mScreenReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (Intent.ACTION_SCREEN_OFF.equals(action)) { if (mSecondsHandler != null) { mSecondsHandler.removeCallbacks(mSecondTick); } } else if (Intent.ACTION_SCREEN_ON.equals(action)) { if (mSecondsHandler != null) { mSecondsHandler.postAtTime(mSecondTick, SystemClock.uptimeMillis() / 1000 * 1000 + 1000); } } } }; private final Runnable mSecondTick = new Runnable() { @Override public void run() { if (mCalendar != null) { updateClock(); } mSecondsHandler.postAtTime(this, SystemClock.uptimeMillis() / 1000 * 1000 + 1000); } }; } 编译报错,lframeworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/clock.java:115: cannot findlsymbolsymbol method getstate()location: class android.view.Display if (getDisplay().getstate() == Display.STATE_ON) fframeworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/clock.java:115: cannot findlsymbolsymbol : variable STATE_ONlocation: class android.view.Display if (getDisplay().getstate() == Display.STATE_ON) fNote: Some input files use or override a deprecated API.Note: Recompile with -xlint:deprecation for details.Note: Some input files use unchecked or unsafe operations.Note: Recompile with -xlint:unchecked for details.2 errorsmake: *** fout/target/common/obi/Apps/SvstemlT intermediates/classes-full-dehun iarl Frror 4l
根据报错信息,可能是因为没有找到 `getstate()` 方法和 `STATE_ON` 变量,这可能是因为你的代码中没有正确引入 `android.view.Display` 类。你可以尝试在代码开头添加以下引入语句:`import android.view.Display;`。另外,你的代码中也存在一些过时的 API 和未检查或不安全的操作,建议使用 `-xlint:deprecation` 和 `-xlint:unchecked` 对代码进行重新编译,以获取更详细的错误信息。
阅读全文