在“氮气简介”中,有这个例子:
body() ->
#panel { style="margin: 50px;", body=[
#h1 { text="My Page" },
#label { text="Enter Your Name:" },
#textbox { },
#button { id=mybutton, text="Submit" }
]}.
然后他们让你添加一个 alert() 的 hello 事件:
body() ->
[
#button { text="Submit", actions=[
#event{type=click, actions=#alert { text="Hello" }
]}
].
所以我想出了这个:
%% -*- mode: nitrogen -*-
%% vim: ts=4 sw=4 et
-module (my_page).
-compile(export_all).
-include_lib("nitrogen_core/include/wf.hrl").
-include("records.hrl").
main() -> #template { file="./site/templates/bare.html" }.
title() -> "Hello from my_page.erl!".
body() ->
wf:wire(#event{
type=click,
trigger=mybutton,
actions="alert('hello')"
}),
#panel{ style="margin: 50px;", body=[
#h1{ text = "My Simple App..." },
#label{ id=mylabel, text="Enter your name:"},
#textbox{ id=mytextbox },
#button{ text="Submit", id="mybutton"}
]}.
指南将向您展示如何提醒()在文本框中输入的名称似乎很自然。我试过了:
body() ->
wf:wire(#event{
type=click,
trigger=mybutton,
actions="alert(obj('mytextbox').text)"
}),
#panel{ style="margin: 50px;", body=[
#h1{ text = "My Simple App..." },
#label{ id=mylabel, text="Enter your name:"},
#textbox{ id=mytextbox },
#button{ text="Submit", id="mybutton"}
]}.
...但是 alert() 说undefined
。我什至找不到该obj()
功能的文档。
我还尝试使用纯 js 进行操作,因为指南说:
什么是氮作用?
操作可以是 Javascript,也可以是呈现为 Javascript 的某些记录。
body() ->
wf:wire(#event{
type=click,
trigger=mybutton,
actions="alert(document.getElementById('mytextbox').innerHTML)"
}),
#panel{ style="margin: 50px;", body=[
#h1{ text = "My Simple App..." },
#label{ id=mylabel, text="Enter your name:"},
#textbox{ id=mytextbox },
#button{ text="Submit", id="mybutton"}
]}.
但是,没有 alert() 弹出。任何人都可以帮忙吗?
编辑: 好吧,当我查看页面源代码时,html 看起来像这样:
<input type="text" class="wfid_temp182655 wfid_mytextbox textbox" value="" />
<input type="button" value="Submit" class="wfid_temp182684 wfid_mybutton button" />
我注意到,即使按钮和文本框记录有一个id
字段,氮也不会为那些 html 元素设置 id 属性——这对我来说毫无意义。