我有一个数组,我想在网页上显示元素,然后将ID动态分配给数组元素(可以通过getElementById访问的ID)。我是javascript和jquery的新手。 需要帮助。
function register() {
var ids = ['name', 'lname', 'email', 'password', 'cpassword'];
var printThis = "";
for (var i = 0; i < ids.length; i++) {
printThis += "<br>" + ids[i];
}
return printThis; // <-- to be printed to the div
}
document.getElementById('ids').innerHTML = register();
<div id="ids"></div>
答案 0 :(得分:0)
尝试一下:-
用于设置ID:
var setIds = $('#mydiv')。attr('id');
var setIds ='#mydiv'+ i
function register() {
var ids = ['name', 'lname', 'email', 'password', 'cpassword'];
var printThis = "";
for (var i = 0; i < ids.length; i++) {
printThis += "<br>" + ids[i];
var setIds= $('#mydiv').attr('id');
var setIds= '#mydiv'+i
}
return printThis; // <-- to be printed to the div
}
document.getElementById('ids').innerHTML = register();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="ids"></div>