分别使用JS和JQ获取想要input元素的方法

<!DOCTYPE html >
< html lang= "en" >

< head >
< meta charset= "UTF-8" >
< meta name= "viewport" content= "width=device-width, initial-scale=1.0" >
< meta http-equiv= "X-UA-Compatible" content= "ie=edge" >
< title >Document </ title >
</ head >
< body >
< input type= "checkbox" class= "inp1" >
< input type= "checkbox" class= "inp2" >
< input type= "radio" class= "inp3" >
< input type= "checkbox" class= "inp4" >
</ body >
< script src= "./jquery-1.12.2.js" ></ script >
< script >
// 在一堆元素中获取固定元素的方法(例子中获取的是type=checkbox的元素)

// 第一种: JS方法
var a= document. getElementsByTagName( "input");
var b=[];
for ( var i = 0; i < a. length; i++) {
if( a[ i]. type== "checkbox"){
b. push( a[ i]);
}
}
console. log( b);
// console.log(b[0]);
// console.log(b[1]);
// console.log(b[2]);

// 第二种: JQ方法
var c= $( 'input:checkbox');
console. log( c);
// console.log(c[0]);
// console.log(c[1]);
// console.log(c[2]);
< / script >

</ html >

猜你喜欢

转载自blog.csdn.net/weixin_41905935/article/details/80888388