Set value/text of a <span> element using Dojo

痞子三分冷 提交于 2020-01-04 04:33:05

问题


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.

I tried:

dojo.byId("hours").value = "00";

Also tried:

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

Here is my fiddle so far.

Please help!


回答1:


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";

or

domAttr.set("hours", "innerHTML", "00");
domAttr.set("hours", "textContent", "00");

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.

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.

I also changed your JSFiddle.




回答2:


If you want to insert text (not HTML) and don't want to worry about carefully escaping problem-characters like <, consider:

dojo.byId("hours").textContent = "00";

This works everywhere except IE8 and below, and unlike innerHtml it should be safe for problematic text like "a

dojo.byId("hours").innerText = "00";

You can see more about compatibility at this page.

In the console, the following error is thrown:

That error refers to network communication, which your code isn't doing. It's probably spurious, something particular to JSFiddle, especially considering the text jobofferinsidebar which sounds like an ad.



来源:https://stackoverflow.com/questions/21014480/set-value-text-of-a-span-element-using-dojo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!