[英]How do I loop through html objects
I'm trying to loop through several objects within the same id
, instead of generating a new id
for each object and writing out the script, as it is shown below. 我试图遍历同一id
内的多个对象,而不是为每个对象生成一个新的id
并写出脚本,如下所示。
<script type="text/javascript">
function get_object(id) {
var object = null;
if (document.layers) {
object = document.layers[id];
} else if (document.all) {
object = document.all[id];
} else if (document.getElementById) {
object = document.getElementById(id);
}
return object;
}
get_object("inputdata").innerHTML=DrawCode39Barcode(get_object("inputdata").innerHTML,1);
get_object("inputdata2").innerHTML=DrawCode39Barcode(get_object("inputdata2").innerHTML,1);
</script>
querySelectorAll
(with a CSS class selector as its argument) and getElementsByClassName
will both return an array-like HTML collection of elements that are a member of a given class. querySelectorAll
(以CSS类选择器为参数)和getElementsByClassName
都将返回元素的类似数组的HTML集合 ,这些元素是给定类的成员。 You can use it with a simple for loop . 您可以将其与简单的for循环一起使用 。
For example looping all Input I use this code 例如循环所有输入我使用此代码
$('input').each(function() {
if(!$(this).val()){
alert('Some fields are empty');
return false;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.