2016-04-13 157 views
1

我有以下的ClojureScript代碼,我試圖檢測點擊的座標。到目前爲止,我甚至無法通過Javascript提醒來識別點擊,更不用說給我一些幫助。檢測點擊座標Clojure

我知道我將不得不編寫一個函數讓它給我確切的細胞被點擊,但作爲一個開始,需要知道如何獲得點擊在頁面上的任何區域的座標。

謝謝!

(defn header [color text] 
    [:h1 
    {:style 
    {:color color 
    :background-color "blue"}} 
    text]) 

(defn Cell [] 
    [:div 
    {:style 
    {:width "40px" 
     :height "40px" 
     :float "right" 
     :margin-bottom "1px" 
     :margin-right "1px" 
     :background-color "grey" 
     :border "1px" "solid" "white"}}]) 

(defn home-page [] 
    [:div 
    [header "red" "Minesweeper"] 
    [:div 
    {:style 
    {:width "440px" 
     :height "440px"}} 
    (repeat 100 [Cell])]]) 

回答

0

把一個:onClick鍵在同一地圖縮進級別爲:style。它的值應該是一個函數,它將得到一個事件e。然後:

(let [coords (.getClientPosition e) 
     coords' {:x (aget coords "x") 
       :y (aget coords "y")}]) 

下面是有一個:onClick事件及其功能一個HashMap的例子:

{ :href "#" 
    :onClick (fn [e] 
       (.preventDefault e) 
       (swap! counter inc))} 

,在重要的唯一的事情上面是獲得e並使用它。這是從flappybird example program,這是我如何開始。

+0

謝謝克里斯,但我得到一個錯誤,說使用未聲明的Var cljs-minesweeper.core/getClientPosition。 –

+0

好吧,我試着將它切換到(js/getClientPosition),並且錯誤消失了,但現在我得到了「使用未聲明的Var cljs-minesweeper.core/e」 –

+0

哦,對不起,我誤解了你的指示。讓我試試看! –