javascript 在dojo javascript框架中通过名称属性获取DOM

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6173843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:46:16  来源:igfitidea点击:

Getting DOM by name attribute in dojo javascript framework

javascriptdomdojo

提问by noname

How to get a DOM using dojo by the tag name?

如何通过标签名称使用 dojo 获取 DOM?

I have a html code like this :

我有一个这样的 html 代码:

<select name="limit">
    <option value="10">10</option>
    <option value="25">25</option>
</select>

in jQuery framework, it will be:

在 jQuery 框架中,它将是:

var limit = $("select[name=limit]");

...but in Dojo framework, what must I do ?

...但在 Dojo 框架中,我必须做什么?

Should I use dojo.query("select[name=limit]")?

我应该使用dojo.query("select[name=limit]")吗?

回答by Frode

Yes, dojo.query("select[name=limit]")is correct, but remember that in dojo, it returns an array (even if there is only one match in the DOM). So to get the first (and possibly only) match, you need select the first element:

是的,dojo.query("select[name=limit]")是正确的,但请记住,在 dojo 中,它返回一个数组(即使 DOM 中只有一个匹配项)。因此,要获得第一个(也可能是唯一的)匹配项,您需要选择第一个元素:

var limit = dojo.query("select[name=limit]")[0];

回答by Daniel Perník

Consider you have an input field named 'myInput'. <input id="1" name="myInput" />

假设您有一个名为“myInput”的输入字段。 <input id="1" name="myInput" />

For getting a value (or other attribute) use following: ([0] define index of your component)

要获取值(或其他属性),请使用以下内容:([0] 定义组件的索引)

dojo.query('[name="myInput"]').attr('value')[0];

If you want to set some value, you will do this:

如果你想设置一些值,你可以这样做:

dojo.query('[name="myInput"]')[0].value = 'newValue';