如何在Javascript中的while循环中编写多个条件?

时间:2017-07-18 15:43:56

标签: javascript

我编写了以下代码来删除theLeftSide和theRightSide的所有子代。但这并不奏效。但是,如果我为每个条件使用一个循环单独写这些条件,它就能正常工作。



while(theLeftSide.firstChild && theRightSide.firstChild)
{
   theLeftSide.removeChild(theLeftSide.firstChild);
   theRightSide.removeChild(theRightSide.firstChild);	   
} 




1 个答案:

答案 0 :(得分:0)

我不确定,这是否是您要实现的目标。可能如果一方中的任何一方没有其他方面的孩子,那么你的逻辑就不起作用了。尝试使用||(OR)运算符而不是&&(AND)。并且您在内部处理删除子功能



while(theLeftSide.firstChild || theRightSide.firstChild) {
    theLeftSide.firstChild ? theLeftSide.removeChild(theLeftSide.firstChild) : null;
    theRightSide.firstChild ? theRightSide.removeChild(theRightSide.firstChild) : null;
}