简体   繁体   English

如何改变 <h1> 标签与ghcjs-dom

[英]how to change <h1> tags with ghcjs-dom

I have found the ghcjs and ghcjs-dom documentation very limited. 我发现ghcjsghcjs-dom文档非常有限。 Here is this basic HTML document. 这是这个基本的HTML文档。

 h1 { font-family: Helvetica; } p {font-family: Helvetica; color: blue; } 
 <h1> Hello World </h1> <p> This is my test document. </p> 

I have read that ghcjs merely compiles Haskell to JavaScript. 我已经读过ghcjs只是将Haskell编译成JavaScript。 If I want to populate the DOM tree even with this simple document, I need to the Foreign Function Interface (FFI) and possibly ghcjs-dom . 如果我想用这个简单的文档填充DOM树,我需要外部函数接口(FFI)和ghcjs-dom

The irony of calling it the " Foreign Function Interface" is that JavaScript is usually considered " native " to the Browser. 将其称为“ 外部函数接口”的讽刺之处在于JavaScript通常被认为是浏览器的“ 本机 ”。 So there is a tiny bit of terminology confusion there. 所以那里有一点点术语​​混乱。

In this very simple example, maybe 在这个非常简单的例子中,也许吧

Let's try a simple example of manipulating the DOM. 让我们尝试一个操作DOM的简单示例。 I have a simple HTML document and I would like to * change the blue paragraph into a red one or * to switch back and forth once each second (between red and blue) 我有一个简单的HTML文档,我想*将蓝色段改为红色或*每秒来回切换一次(红色和蓝色之间)

I don't see how the ghcjs set of tools will achieve harder tasks if it cannot even do these very basic test case and explain it. 如果它甚至不能完成这些非常基本的测试用例并解释它,那么ghcjs工具集将如何实现更难的任务。 Here is an issue I have raised on Github, with the conclusion that ghcjs lacks a good on-boarding process. 这是我在Github上提出的一个问题,结论是ghcjs缺乏良好的入职流程。

Here's a short self-contained example that uses reflex-dom to do the red/blue color switching that you described. 这是一个简短的自包含示例,它使用reflex-dom进行您描述的红/蓝色切换。 This is a modified version of the code that epsilonhalbe included in this answer to your earlier question . 这是epsilonhalbe包含在您之前问题的 答案中的代码的修改版本。

{-# LANGUAGE OverloadedStrings #-} 
{-# LANGUAGE ScopedTypeVariables #-} -- allows for local type declarations.
import Reflex
import Reflex.Dom
import Data.Text (Text, pack)
import Data.Map (Map)
import Data.Time.Clock (getCurrentTime)
import Control.Monad.Trans (liftIO)

webPage :: MonadWidget t m => m ()
webPage = do

  -- ticker Event fires once per second.
  ticker :: Event t TickInfo <- tickLossy 1.0 =<< liftIO getCurrentTime  

  -- counter Dynamic increases by one every second.
  counter :: Dynamic t Int <- foldDyn  (\_ n -> n+1) 0 ticker

  -- function to map from integer to red or blue style.
  let chooseColor :: Int -> (Map Text Text) 
      chooseColor n = "style" =: pack ("color: " ++ if (n `mod` 2) == 0 then "red" else "blue")

  -- redBlueStyle Dynamic changes each second.
  let redBlueStyle :: Dynamic t (Map Text Text) 
      redBlueStyle = fmap chooseColor counter

  -- insert an h1 elemnt.
  el "h1" $ text "Hello World!"

  -- insert a paragraph with Dynamic red blue style.
  elDynAttr "p" redBlueStyle $ text "This is my test document"

  return ()


css = "h1 {font-family: Helvetica;} p {font-family: Helvetica;}" 

main :: IO ()
main = mainWidgetWithCss css webPage

Of course, reflex-dom (along with reflex) is a higher level library than ghcjs-dom and it comes with its own set of concepts (Event, Dynamic, Behavior, etc) that you need to get comfortable with. 当然,reflex-dom(以及反射)是一个比ghcjs-dom更高级别的库,它有自己的一套概念(事件,动态,行为等),你需要熟悉它们。

The example works by creating a Dynamic Map that specifies a style that alternates from red to blue each second and using that Dynamic Map to style an element. 该示例的工作原理是创建一个动态地图,指定每秒从红色到蓝色交替的样式,并使用该动态地图为元素设置样式。

For clarity's sake, this example contains some type declarations that are not strictly required. 为清楚起见,此示例包含一些非严格要求的类型声明。

This project: https://github.com/dc25/stackOverflow__how-to-change-h1-tags-with-ghcjs-dom contains the above code. 该项目: https//github.com/dc25/stackOverflow__how-to-change-h1-tags-with-ghcjs-dom包含上述代码。 Here is a link to a browser based demo: https://dc25.github.io/stackOverflow__how-to-change-h1-tags-with-ghcjs-dom/ 以下是基于浏览器的演示的链接: https//dc25.github.io/stackOverflow__how-to-change-h1-tags-with-ghcjs-dom/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM