设置< span>的值/文本元素使用Dojo [英] Set value/text of a <span> element using Dojo
问题描述
我正在开发一个秒表应用程序,试图学习Dojo Toolkit。所以,首先,我需要将时间,分钟,秒和毫秒设置为0。
I am developing a stopwatch application in an attempt to learn the Dojo Toolkit. So, to start with, I need to set the hours, minutes, seconds and milliseconds to 0.
我试过:
dojo.byId("hours").value = "00";
还试过:
domAttr.set("hours", 00);
它没有工作。在控制台中,抛出以下错误:
It didnt work. In the console, the following error is thrown:
GET http://jobs.jsfiddle.net/random.js?callback=Request.JSONP.request_map.request_0 500 (Internal Server Error) moo-clientcide-1.3.js?jobofferinsidebar:3146
请帮助!
推荐答案
那是因为值
在使用表单字段时使用。如果要替换DOM节点的实际内容,您可以使用 innerHTML
或 textContent
。例如:
Well, that's because value
is only used when working with form fields. If you want to replace the actual content of the DOM node, you use innerHTML
or textContent
in stead. For example:
dojo.byId("hours").innerHTML = "00";
dojo.byId("hours").textContent = "00";
或
domAttr.set("hours", "innerHTML", "00");
domAttr.set("hours", "textContent", "00");
innerHTML
和 textContent
是后者只允许文本内容(如属性说),而 innerHTML
也允许输入HTML。如果你不信任输入,你一定要使用 textContent
。
The difference between innerHTML
and textContent
is that the latter only allows text content (like the property says), while innerHTML
also allows to input HTML. If you don't trust the input, you should definitely be using textContent
.
注意:您需要在 00
之间放置引号,因为它将被解释为数值,这意味着第一个 0
在您输出时被跳过。
Be aware: you need to put quotes around the 00
because else it will be interpreted as a numeric value, which means the first 0
is skipped when you output it.
我还更改了您的 JSFiddle 。
这篇关于设置< span>的值/文本元素使用Dojo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!