我想我的问题很简单,但是我是javascript的真正初学者,找不到我想要的东西:
当鼠标悬停在导航或ul上时,我试图获取li的ID。我的HTML结构为:
<nav><ul id="menu">
<li id="FirstLink">Link1</li>
<li id="SecondLink">Link2</li>
<li id="ThirdLink">Link3</li>
</ul></nav>
Run Code Online (Sandbox Code Playgroud)
所以我的目标是在每个li上监听一个mouseover(和mouseout)事件,但是一个包含10个监听器(对于5 li)的脚本实在是太脏了……
这就是为什么我想到这样的脚本:
var menu = document.getElementById("menu");
menu.addEventListener('mouseover', myFunction, false);
function myFunction () {
//something that get the ID of the <li> that is currently under the mouse and can put it inside a variable as "Link1"
}
Run Code Online (Sandbox Code Playgroud)
但是,如果有更好的解决方案,我将很高兴知道!(我想留在纯js)
我正在学习python并且有一个练习,我有一个整数列表,并且必须保持两个数字,然后2个3,然后3个4 ...然后看到剩下的结果.例如:[1,2,3,4,5,6,7,8,9] - > [1,3,5,7,9] - > [1,3,7,9] - > [1,3,7]
我的第一次尝试,只是为了看到输出(小心,令人震惊的脏代码):
n=input()
list2=list(range(1,int(n)))
list1=[]
step=2
while list1 != list2 :
countSteps=1
position=0
list1=list2
list2=[]
lenlist1=len(list1)
while position < lenlist1 :
while countSteps != step and position < lenlist1 :
list2.append(list1[position])
countSteps+=1
position+=1
position+=1
countSteps = 1
step+=1
print(list2)
Run Code Online (Sandbox Code Playgroud)
"想法"是使用两个列表,并在2/3/4 ......上添加1/2/3 ...数字从一个到另一个.在我看来(也许我错了)记忆明智这是一个糟糕的选择,所以我想出了以下内容:
n=input()
list1=list(range(1,int(n)))
lenCached=0
step=1
while lenCached!=len(list1) :
lenCached = len(list1)
position = step
while position < len(list1):
list1.pop(position)
position+=step
step+=1
print(list1)
Run Code Online (Sandbox Code Playgroud)
我很高兴这最后一个看起来如何,但表现是可怕的.当我运行第一个范围(1,1000000)时,它需要10秒,而第二个需要年龄(几分钟).为什么我能做些什么呢?
(如果读到这个,你想到了一个更好的方法来实现最初的目标,我当然会有兴趣听到它!)