简体   繁体   English

此代码Javascript和Parse有什么问题

[英]What is wrong with this code Javascript and Parse

I am using javascript and the parse API and i have this code 我正在使用javascript和parse API,并且我有此代码

<script type="text/javascript">
Parse.initialize("APPID", "JAVASCRIPTCODE");
var currentUser = Parse.User.current().get("FirstName");
document.getElementById("fname").value = currentUser;
</script>

<input type=text id="fname" />

however the code is not adding the data from javascript to the text box where am i going wrong. 但是代码没有将来自javascript的数据添加到文本框中,这是我出错的地方。

The problem is that you're accessing the element before it is added into DOM . 问题是您在将元素添加到DOM之前正在访问它。

You can wrap the last statement in DOMContentLoaded 您可以将最后一条语句包装在DOMContentLoaded

The DOMContentLoaded event is fired when the document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading (the load event can be used to detect a fully-loaded page). 当文档已完全加载和解析时,将触发DOMContentLoaded事件,而无需等待样式表,图像和子帧完成加载(可以使用load事件来检测完全加载的页面)。

document.addEventListener("DOMContentLoaded", function(event) {
    document.getElementById("fname").value = fname;
});

Or, you can also move your <script> at the end of <body> tag. 或者,您也可以将<script>移到<body>标记的末尾。

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

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