我有一个向量:
(def my-collection ["image1.jpg" "image2.jpg" "image3.jpg"])我想在文件中制作3张图片。
(println (count my-collection)) ; this prints count of my collection. This is 3.
(map (fn [x] (println x)) my-collection) ; doesn't do anything!但!
(def image-element (.createElement js/document "img"))
(def insert-into-body (.appendChild (.-body js/document) image-element))
(set! (.-src image-element) "image1.jpg")这段代码非常适合一个元素!
我该做些什么来收集?
发布于 2015-09-07 09:34:17
map函数用于通过应用指定的函数将集合转换为另一个集合。因为(println x)返回nil,所以代码的结果将是具有副作用的(nil, nil, nil) (每个图像名都打印在控制台中)。
也许您希望定义一个函数来创建具有指定src的图像元素。
(defn create-image [src]
(let [img (.createElement js/document "img")]
(set! (.-src img) src)
img))现在,您可以提供集合来将图像名映射到图像元素,然后将它们附加到body元素中。
(doseq [i (map create-image my-collection)]
(.appendChild (.-body js/document) i))https://stackoverflow.com/questions/32434643
复制相似问题