Javascript:遍历页面中的所有控件 [英] Javascript: Loop through all controls in the page

查看:63
本文介绍了Javascript:遍历页面中的所有控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我需要有关Javascript代码的帮助。我想要做的基本上是遍历页面中的所有控件。该页面包含table,gridview with itemtemplates等。我想为元素类型文本框设置一定的值,为元素类型标签设置另一个值。

我的代码看起来像这样:

Hello,

I need help with Javascript code. What i want to do basically is to loop through all controls in the page. The page contains table, gridview with itemtemplates etc. I want to set certain value for element type textbox and another value for elementtype label.
my code looks something like this:

function postExecute() {
       for (var i = 0; i < document.forms[0].elements.length; i++) {
           element = document.forms[0].elements[i];

           switch (element.type) {
               case 'text':
                   if (element.value.length > 0) {
                       element.value =  element.value.replace("<", '<'); //do htmlDecode
                   }
                   break;
               case 'span':
                   if (element.value.length > 0) {
                       element.value = element.value.replace("<", '<');
                       //do htmlcode.
                   }
                   break;
           }
       }

   }







如果我只放置元素类型文本。有用。我希望它适用于elementtype标签。并非所有标签都有跨度。 javascript中是否有元素类型标签?或者是什么方法来完成这个?




If I put only for elementtype text. It works. I want it to work for elementtype label. not all labels have a span. Is there an elementtype "label" in javascript?? or what would be a way to get this done??

推荐答案

这是一个使用 jQuery 来查找所有的示例文本框元素



Here's a sample using jQuery to locate all Textbox elements


' 表单输入[type =text]')。each( function (){
// 在这里处理每个文本框的内容,例如:
if this .value.match(/ \ D /)) // 仅限数字的正则表达式。
Foo();
});
('form input[type="text"]').each(function(){ // Do stuff with each of the Textboxes here e.g.: if (this.value.match(/\D/)) // regular expression for numbers only. Foo(); });







* 了解有关jQuery的更多信息 [ ^ ]



干杯,

Edo




* To learn more about jQuery[^]

Cheers,
Edo


所有元素都有标签名称。因此,您可以通过标记名获取任何元素。

,例如您希望获得带有标记INPUT的所有元素,然后使用 getelementsbytagname 函数。



您正在寻找TYPE属性。 TYPE属性仅适用于元素 INPUT



SPAN是一个元素。它没有任何属性名称TYPE。现在,如果您想知道元素的类型,请使用 tagName 。即 element.tagName 。关于Label的同样的东西。您没有TYPE属性。但是使用tagName你可以弄清楚你在处理什么类型的元素。
all element has tag name. So you can get any element by tagname.
such as you want to get all the element with tag INPUT then use getelementsbytagname function.

You are looking for TYPE attribute. TYPE attribute is only available for element INPUT

SPAN is a ELEMENT. It does not have any attribute name TYPE. Now if you want to know the type of element then use tagName. i.e. element.tagName. samething about Label. You do not have TYPE attribute. But using tagName you can figure out what type of element are you dealing with.


这篇关于Javascript:遍历页面中的所有控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆