问题描述
很难用Elm导出console.log。
我想通过按下下面的按钮并使用控制台登录功能来输出控制台日志。我该怎么办?
clasp status
如果我使用Javascript,我想做下面的事情。
<!DOCTYPE html>
<html>
<head>
<Meta name="viewport" content="width=device-width,initial-scale=1">
<style>
body {font-family: Arial,Helvetica,sans-serif;}
$(":checkBox").on('click',function(){
$('.proceed').toggleClass("checked");
});
.proceed{
display:none;
}
.checked{
display:block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
.modal {
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0,0);
background-color: rgba(0,0.4);
}
.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 1px solid #888;
width: 80%;
Box-shadow: 0 4px 8px 0 rgba(0,0.2),0 6px 20px 0 rgba(0,0.19);
-webkit-animation-name: animatetop;
-webkit-animation-duration: 0.4s;
animation-name: animatetop;
animation-duration: 0.4s
}
@-webkit-keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
@keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
.close {
color: white;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.modal-header {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}
.modal-body {padding: 2px 16px;}
.modal-footer {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}
</style>
</head>
<body>
<h2>Animated Modal with Header and Footer</h2>
<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<h2>Modal Header</h2>
</div>
<div class="modal-body">
<p>Some text in the Modal Body</p>
<p>Some other text...</p>
</div>
<div class="modal-footer">
<h3>Modal Footer</h3>
</div>
</div>
</div>
<input type="checkBox" id="tos" onchange="changeTriggered()"> I Agree to the <a href="">Terms and Conditions</a> and <a href="">Privacy Policy</a>.
<br>
<button id="myBtn" class="proceed">Open Modal</button>
<script>
var modal = document.getElementById("myModal");
var btn = document.getElementById("myBtn");
var span = document.getElementsByClassName("close")[0];
btn.onclick = function() {
modal.style.display = "block";
}
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
function changeTriggered() {
let tos = document.getElementById("tos");
console.log(tos.checked);
if(tos.checked) {
modal.style.display = "block";
}
}
</script>
</body>
</html>
解决方法
如果仅用于调试,则可以使用Debug.log
,但是不能在生产代码中使用Debug
模块(如果您使用--optimize
运行编译器将无法编译有Debug
个用法)。使用它可能像这样:
sampleView : Html Message
sampleView =
Html.div (class "sample-card" :: Styles.sampleCard)
[ Html.div
(class "sample-card-list" :: Styles.sampleCardList)
[
Html.button
([class "sample-card-button",onClick CardButtonClicked] ++ Styles.sampleCardListButton)
[
Html.text "サンプルボタン"
]
]
]
update : Message -> Model -> ( Model,Cmd Message )
update msg model =
case msg of
CardButtonClicked ->
let
_ = Debug.log "クリックされました" msg
in
( model,Cmd.none )
如果要在生产应用程序中写入控制台,则需要use a port。要创建端口,您需要更新其所在的模块以声明为port module
(只需在文件第一行的模块声明的开头添加port
)。 / p>
port module Main exposing (..)
port consoleLog : String -> Cmd msg
update : Message -> Model -> ( Model,Cmd Message )
update msg model =
case msg of
CardButtonClicked ->
( model,consoleLog "クリックされました" )
然后,您需要连接一些JavaScript来处理该端口消息:
var app = Elm.Main.init({ node: document.querySelector('main') })
app.ports.consoleLog.subscribe(function (msg) {
console.log(msg);
});
Here's an example app using a port呼叫console.log
。