问题描述
函数authorNumbering(){
VAR authorDivs =的document.getElementById('authorView')的getElementsByTagName(格)。
变种标签= [];
VAR nameForLabels = [作者1,作者2,作者3,4页,作者5,6作者,作者7];
对于(VAR I = 0; I< authorDivs.length;我++){
VAR labelsFromDiv = authorDivs [I] .getElementsByTagName(标签);
为(变量X = 0; X&下; labelsFromDiv.length; X ++){
如果(!(typeof运算labelsFromDiv.item(X)===未定义|| labelsFromDiv.item(X)== NULL)){
标签[X] = labelsFromDiv.item(x)的; }
}
标签[I] .innerHTML = nameForLabels [I]
}
}
本质功能(或者至少是它在我的头)通过进入和从作者的div得到了一堆标签。我然后保存标签(这是不是undefined或null)到一个数组。我现在想能够改变的标签,上面显示(按 X
),但由于某种原因, X
是总是0或1。我试过一对夫妇的事情,但我不认为我理解的逻辑不够正确看着办吧。 标签[]
也有在阵列内重复的,我怎么能避免同时还具有两个循环重复?
我是新来的JavaScript,但我想我最好的。
编辑1:哇,我真的搞砸了什么,我试图在那里做。感谢您的帮助,我更新了code。
我想要做的就是从 nameForLabels
阵列标签重命名的标签。这适用于第一个标签但后来我收到以下错误:类型错误:标签[I]未定义
这是发生,因为标签
不超过1的长度,我觉得它就会重写每一次,而不是被写入到阵列中的下一个位置。为什么 X
从不大于0?
我想看到的标签
阵列得到改变,以那些在 nameForLabels $ C $的所有标签是C>阵列。例如,在
标签[0]
将成为作者1
和标签在标签[1 ]
将成为作者2
等。
编辑2:添加HTML例如:
< DIV的风格=显示:块; ID =authorView级=形横>
< DIV ID =author0级=表单组>
< DIV CLASS =COL-SM-4控制标签><标签类=为=author0ID =lblauthor0>作者1:< /标签>< / DIV>
< DIV CLASS =COL-SM-8>
< DIV CLASS =输入组><输入类=表格控... *保持与不相关的信息去*
< / DIV>
< / DIV>
< DIV ID =作者1级=表单组>
< DIV CLASS =COL-SM-4控制标签><标签类=为=作者1ID =lblauthor1>作者2:< /标签>< / DIV>
< DIV CLASS =COL-SM-8>
< DIV CLASS =输入组><输入类=表格控... *保持与不相关的信息去*
< / DIV>
< / DIV>
< DIV ID =author2级=表单组>
< DIV CLASS =COL-SM-4控制标签><标签类=为=author2ID =lblauthor2>作者3:LT; /标签>< / DIV>
< DIV CLASS =COL-SM-8>
< DIV CLASS =输入组><输入类=表格控... *保持与不相关的信息去*
< / DIV>
< / DIV>
< DIV ID =author3级=表单组>
< DIV CLASS =COL-SM-4控制标签><标签类=为=author3ID =lblauthor3>作者4:LT; /标签>< / DIV>
< DIV CLASS =COL-SM-8>
< DIV CLASS =输入组><输入类=表格控... *保持与不相关的信息去*
< / DIV>
< / DIV>
< DIV ID =author4级=表单组>
< DIV CLASS =COL-SM-4控制标签><标签类=为=author4ID =lblauthor4>作者5:LT; /标签>< / DIV>
< DIV CLASS =COL-SM-8>
< DIV CLASS =输入组><输入类=表格控... *保持与不相关的信息去*
< / DIV>
< / DIV>
< / DIV>
我不认为这正是你的code,因为你不能有一个函数名循环......除此之外,它的工作
<脚本类型=文/ JavaScript的> 功能FindGoodFunctionName(){
VAR authorDivs =的document.getElementById('authorView')的getElementsByTagName(格)。
变种标签= []; 对于(VAR I = 0; I< authorDivs.length;我++){
VAR labelsFromDiv = authorDivs [I] .getElementsByTagName(标签);
为(变量X = 0; X&下; labelsFromDiv.length; X ++){
// 只是不 ! if语句,也不需要有一个空单
如果(typeof运算labelsFromDiv.item(X)===未定义|| labelsFromDiv.item(X)== NULL){}
其他{
标签[X] = labelsFromDiv.item(x)的;
标签[X] .innerHTML =作者1;
标签[X] .innerHTML =作者2;
标签[X] .innerHTML =作者3; //这是奇怪的,分配3个不同的值相同的标签
}
}
}
}< / SCRIPT>< DIV ID =authorView>
<div><label></label><label></label><label></label></div>
<div><label></label><label></label><label></label></div>
<div><label></label><label></label><label></label></div>
&LT; / DIV&GT;&LT;输入类型=按钮的onclick =FindGoodFunctionName();值=测试/&GT;
更新
下面是它如何能看起来像你的HTML。
函数FindGoodFunctionName(){
VAR authorLabels =的document.getElementById('authorView')的getElementsByTagName(标签)。
变种标签= []; 对于(VAR I = 0; I&LT; authorLabels.length;我++){
标签由[i] = authorLabels [I]
标签[I] .innerHTML =AAAA;
}
}
function authorNumbering() {
var authorDivs = document.getElementById('authorView').getElementsByTagName("div");
var labels = [];
var nameForLabels = ["Author 1", "Author 2", "Author 3", "Author 4", "Author 5", "Author 6", "Author 7"];
for (var i = 0; i < authorDivs.length; i++) {
var labelsFromDiv = authorDivs[i].getElementsByTagName("label");
for (var x = 0; x < labelsFromDiv.length; x++) {
if (!(typeof labelsFromDiv.item(x) === "undefined" || labelsFromDiv.item(x) == null)) {
labels[x] = labelsFromDiv.item(x);
}
}
labels[i].innerHTML = nameForLabels[i];
}
}
Essentially the function (or at least what it does in my head) goes through and gets a bunch of labels from the author divs. I then save the labels (which are not undefined or null) into an array. I now would like to be able to change the labels as shown above (at x
) but for some reason x
is always either 0 or 1. I've tried a couple things but I don't think I understand the logic enough to figure it out properly. labels[]
also has duplicates within the array, how can I avoid having duplicates while still having two loops?
I'm new to JavaScript but I'm trying my best.
Edit 1: Wow, I really messed up what I was trying to do there. Thanks for the help I've updated the code.
What I want to do is rename the labels with a label from nameForLabels
array. This works for the first label but then I receive the following error: TypeError: labels[i] is undefined
This is happening because labels
never exceeds the length of 1, I think it gets rewritten every time rather than get written to the next position in the array. How come x
never goes higher than 0?
What I would like to see is all the labels in the labels
array get changed to those that are in the nameForLabels
array. For example the label in labels[0]
would become Author 1
, and labels[1]
would become Author 2
and so on.
Edit 2: HTML example added:
<div style="display: block;" id="authorView" class="form-horizontal">
<div id="author0" class="form-group">
<div class="col-sm-4 control-label"><label class="" for="author0" id="lblauthor0">Author 1:</label></div>
<div class="col-sm-8">
<div class="input-group"><input class="form-control" ... *keeps going with irrelevant information*
</div>
</div>
<div id="author1" class="form-group">
<div class="col-sm-4 control-label"><label class="" for="author1" id="lblauthor1">Author 2:</label></div>
<div class="col-sm-8">
<div class="input-group"><input class="form-control" ... *keeps going with irrelevant information*
</div>
</div>
<div id="author2" class="form-group">
<div class="col-sm-4 control-label"><label class="" for="author2" id="lblauthor2">Author 3:</label></div>
<div class="col-sm-8">
<div class="input-group"><input class="form-control" ... *keeps going with irrelevant information*
</div>
</div>
<div id="author3" class="form-group">
<div class="col-sm-4 control-label"><label class="" for="author3" id="lblauthor3">Author 4:</label></div>
<div class="col-sm-8">
<div class="input-group"><input class="form-control" ... *keeps going with irrelevant information*
</div>
</div>
<div id="author4" class="form-group">
<div class="col-sm-4 control-label"><label class="" for="author4" id="lblauthor4">Author 5:</label></div>
<div class="col-sm-8">
<div class="input-group"><input class="form-control" ... *keeps going with irrelevant information*
</div>
</div>
</div>
I don't think that is exactly your code since you can't have a function name loop... Apart from that, it does work.
https://jsfiddle.net/utswp2v0/
<script type="text/javascript">
function FindGoodFunctionName() {
var authorDivs = document.getElementById('authorView').getElementsByTagName("div");
var labels = [];
for (var i = 0; i < authorDivs.length; i++) {
var labelsFromDiv = authorDivs[i].getElementsByTagName("label");
for (var x = 0; x < labelsFromDiv.length; x++) {
// Just "Not" ! the if statement, don't need to have a empty one
if (typeof labelsFromDiv.item(x) === "undefined" || labelsFromDiv.item(x) == null) { }
else {
labels[x] = labelsFromDiv.item(x);
labels[x].innerHTML = "Author 1";
labels[x].innerHTML = "Author 2";
labels[x].innerHTML = "Author 3"; // This is weird, assigning 3 different value to the same label
}
}
}
}
</script>
<div id="authorView">
<div><label></label><label></label><label></label></div>
<div><label></label><label></label><label></label></div>
<div><label></label><label></label><label></label></div>
</div>
<input type="button" onclick="FindGoodFunctionName();" value="test" />
Update
Here's how it can look like with your html.
function FindGoodFunctionName() {
var authorLabels = document.getElementById('authorView').getElementsByTagName("label");
var labels = [];
for (var i = 0; i < authorLabels.length; i++) {
labels[i] = authorLabels[i];
labels[i].innerHTML = "aaaa";
}
}
这篇关于JavaScript的循环逻辑是不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!