在javascript上按类型访问元素

时间:2022-10-23 21:35:51

A while ago I was making some test on javascript, and played with a code to get the text of all elements with a certain class, Now I was trying to make something like this but obtain all elements by a certain type, for example all elements type="text" Is there any way to do this in javascript or should I use jquery?

前段时间我正在对javascript进行一些测试,并使用代码来获取具有某个类的所有元素的文本,现在我试图制作这样的东西,但是获得某种类型的所有元素,例如所有元素type =“text”有没有办法在javascript中执行此操作或我应该使用jquery?

var xx = document.getElementsByClassName("class");
for (i=0;i<xx.length;i++){
    var str=xx[i].innerHTML;
            alert(str);
}

Thanks-

4 个解决方案

#1


49  

In plain-old JavaScript you can do this:

在普通的JavaScript中,您可以这样做:

var inputs = document.getElementsByTagName('input');

for(var i = 0; i < inputs.length; i++) {
    if(inputs[i].type.toLowerCase() == 'text') {
        alert(inputs[i].value);
    }
}

In jQuery, you would just do:

在jQuery中,您只需:

// select all inputs of type 'text' on the page
$("input:text")

// hide all text inputs which are descendants of div class="foo"
$("div.foo input:text").hide();

#2


39  

If you are lucky and need to care only for recent browsers, you can use:

如果您很幸运并且只需要关注最近的浏览器,您可以使用:

document.querySelectorAll('input[type=text]')

"recent" means not IE6 and IE7

“最近”意味着不是IE6和IE7

#3


2  

var inputs = document.querySelectorAll("input[type=text]") ||
(function() {
    var ret=[], elems = document.getElementsByTagName('input'), i=0,l=elems.length;
    for (;i<l;i++) {
        if (elems[i].type.toLowerCase() === "text") {
            ret.push(elems[i]);
        }
    }

    return ret;
}());

#4


1  

The sizzle selector engine (what powers JQuery) is perfectly geared up for this:

sizzle选择器引擎(JQuery的功能)完全可以满足于此:

var elements = $('input[type=text]');

Or

var elements = $('input:text');

#1


49  

In plain-old JavaScript you can do this:

在普通的JavaScript中,您可以这样做:

var inputs = document.getElementsByTagName('input');

for(var i = 0; i < inputs.length; i++) {
    if(inputs[i].type.toLowerCase() == 'text') {
        alert(inputs[i].value);
    }
}

In jQuery, you would just do:

在jQuery中,您只需:

// select all inputs of type 'text' on the page
$("input:text")

// hide all text inputs which are descendants of div class="foo"
$("div.foo input:text").hide();

#2


39  

If you are lucky and need to care only for recent browsers, you can use:

如果您很幸运并且只需要关注最近的浏览器,您可以使用:

document.querySelectorAll('input[type=text]')

"recent" means not IE6 and IE7

“最近”意味着不是IE6和IE7

#3


2  

var inputs = document.querySelectorAll("input[type=text]") ||
(function() {
    var ret=[], elems = document.getElementsByTagName('input'), i=0,l=elems.length;
    for (;i<l;i++) {
        if (elems[i].type.toLowerCase() === "text") {
            ret.push(elems[i]);
        }
    }

    return ret;
}());

#4


1  

The sizzle selector engine (what powers JQuery) is perfectly geared up for this:

sizzle选择器引擎(JQuery的功能)完全可以满足于此:

var elements = $('input[type=text]');

Or

var elements = $('input:text');