简体   繁体   English

Purescript 中的 getElementById

[英]getElementById in Purescript

I am very new to Purescript so this might be a naive question.我对 Purescript 很陌生,所以这可能是一个幼稚的问题。

I want to write a Purescript function that reads input from HTML input elements on the browser and writes some output to another HTML input element. I want to write a Purescript function that reads input from HTML input elements on the browser and writes some output to another HTML input element.

With plain Javascript it's as simple as document.getElementById('output').value = myFun(document.getElementById('input'));使用普通的 Javascript 就像document.getElementById('output').value = myFun(document.getElementById('input')); . . How do I do this with just Purescript?我如何只用 Purescript 做到这一点?

EDIT:编辑:

I've noticed that my answer doesn't meet the requirements - I'm only setting up an element value.我注意到我的答案不符合要求 - 我只是设置一个元素值。 If I find more time I probably add also reading from element value piece but you should be able to guess how to do this from the hints provided already:-)如果我找到更多时间,我可能还会添加从元素值中读取的内容,但是您应该能够从已经提供的提示中猜出如何做到这一点:-)


In general when using PureScript you want to use some high level framework to manipulate the DOM like: halogen, react-basic, concur, spork, elmish, flare, hedwig, flame (for sure I've missed some others - sorry about that).一般来说,在使用 PureScript 时,您希望使用一些高级框架来操作 DOM,例如:halogen、react-basic、concur、spork、elmish、flare、hedwig、flame(当然我错过了其他一些 - 抱歉) .

But if you really want to mutate the DOM by hand please don't be surprised that it is not as pleasant experience as in imperative JavaScript.但是,如果您真的想手动修改 DOM,请不要感到惊讶,因为它不像命令式 JavaScript 那样令人愉快。 It is on purpose - PureScript has the power to separate effects from pure functions and we have to work with Effect in every step here.这是有目的的——PureScript 有能力将效果与纯函数分开,我们必须在每一步都使用Effect On the other hand this gives us an unique ability to reason about the code and be sure where side effects can happen and which parts of our program are pure.另一方面,这给了我们一种独特的能力来推理代码并确定副作用可能发生在哪里以及我们程序的哪些部分是纯的。

So let's use low level purescript-web-html .所以让我们使用低级别的purescript-web-html This library is low level but provides strict types around DOM API so like I said it requires quite a lot of manual value passing:这个库是低级的,但是围绕 DOM API 提供了严格的类型,所以就像我说的那样,它需要大量的手动值传递:

module Main where

import Prelude

import Data.Maybe (Maybe(..))
import Effect (Effect)
import Web.DOM.Document (toNonElementParentNode)
import Web.DOM.Element (setAttribute)
import Web.DOM.NonElementParentNode (getElementById)
import Web.HTML (window)
import Web.HTML.HTMLDocument (toDocument)
import Web.HTML.Window (document)

main :: Effect Unit
main = do
  w ← window
  d ← document w
  maybeElement ← getElementById "test-input" $ toNonElementParentNode $ toDocument  d
  case maybeElement of
    Nothing → pure unit
    Just elem → do
      setAttribute "value" "new-value" elem

This can be written a bit shorter using point free style so avoiding intermediate variables:这可以使用无点样式写得更短一些,从而避免中间变量:

main :: Effect Unit
main = window >>= document >>= toDocument >>> toNonElementParentNode >>> getElementById "test-input" >>= case _ of
  Nothing → pure unit
  Just elem → setAttribute "value" "new-value" elem

Direct DOM manipulation is probably not the best way to start building a larger project or beginning the adventure with this really wonderful language.直接 DOM 操作可能不是开始构建更大的项目或使用这种非常棒的语言开始冒险的最佳方式。 On the other hand it can be useful from time to time;-)另一方面,它有时会很有用;-)

I used Foreign Function Interface (FFI) feature of Purescript as follows.我使用 Purescript 的 Foreign Function Interface (FFI) 功能如下。

Define your Purescript module with imports of foreign functions you want to use.使用您要使用的外部函数的导入来定义您的 Purescript 模块。 Here we have imported two functions.这里我们导入了两个函数。

-- Main.purs
foreign import getElementById :: String -> Effect String
foreign import setElementById :: String -> String -> Effect Unit 

Now create a Javascript file with the same name but .js extension.现在创建一个具有相同名称但扩展名为.js的 Javascript 文件。 We will export JS functions from here for use in Purescript.我们将从这里导出 JS 函数以在 Purescript 中使用。

// Main.js
"use strict";

exports.getElementById = function(id) {
    return document.getElementById(id).value;
};

exports.setElementById = function(id) {
    return function(value) {
    document.getElementById(id).value = value;
    };
};

Now we can call getElementById and setElementById functions in our Purescript files.现在我们可以在 Purescript 文件中调用getElementByIdsetElementById函数。

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

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