我可以将document.getElementById的内容应用于document.getElementsByClassName吗?

时间:2015-11-04 13:55:35

标签: javascript

document.getElementById("myId").style.left = 100px;

是否有一种简单的方法可以将上面的代码用于某个类?

以下代码是否有效?

doucment.getElementsByClassName("myClass").style.left = 100px;

我想要做的是将多个图像移动到同一个地方,它应该是灵活的,这样如果用户想要移动到那个地方的1,2,3或更多图像,它就会起作用。

2 个答案:

答案 0 :(得分:3)

import cv2 def frame_capture: cap = cv2.VideoCapture("video.mp4") while not cap.isOpened(): cap = cv2.VideoCapture("video.mp4") cv2.waitKey(1000) print "Wait for the header" pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) while True: flag, frame = cap.read() if flag: # The frame is ready and already captured cv2.imshow('video', frame) pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) print str(pos_frame)+" frames" else: # The next frame is not ready, so we try to read it again cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, pos_frame-1) print "frame is not ready" # It is better to wait for a while for the next frame to be ready cv2.waitKey(1000) if cv2.waitKey(10) == 27: break if cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) == cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT): # If the number of captured frames is equal to the total number of frames, # we stop break 函数返回一个NodeList,因此您需要迭代它并依次将样式应用于每个元素。使用getElementsByClassName()相对容易,但您需要先转换为数组:

forEach

答案 1 :(得分:2)

是的,但getElementsByClassName返回类似于数组的对象(HTMLCollection),因此您需要指定要应用更改的元素。例如:

document.getElementsByClassName("myClass")[0].style.left = 100px;

将应用于该类的第一个元素。否则你需要遍历集合。