javascript - 如何显示每个数组的动态文本?

标签 javascript

通过按下一步按钮,文本应该动态更改。我为此使用数组。现在顺序不正确。

在下面的示例中,我尝试显示每个数组的文本,目前这仍然是错误的。文本应如下所示:

参数 arr1,1 参数 arr1,2 参数 arr1,3

按下Next-Button后,应该加载下一个数组。那应该看起来像这样:

参数 arr2,1 参数 arr2,2 参数 arr2,3

有人有解决办法吗?谢谢。

//////////Text Array1//////////
	
var arr1 = [
	"Argument arr1,1",
	"Argument arr1,2",
	"Argument arr1,3",
	"Argument arr1,4",
];

var i1 = -1;
	
function nextItem1() {		
	i1 = i1 + 1; 	
	i1 = i1 % arr1.length 
		
  return arr1[i1]	
}

function prevItem1() {
	if (i1 === 0) { 
		  i1 = arr1.length 		
}
	
	i1 = i1 - 1; 
	return arr1[i1];	
}

window.addEventListener('load', function () {		
	document.getElementById.textContent = arr1[0]; 	
	document.getElementById('prev_button').addEventListener(	
		'click', 		
		function (e) { 
			document.getElementById('arr1').textContent = prevItem1();				
	}			
);
    
	document.getElementById('next_button').addEventListener(
		'click', 
		function (e) { 
			document.getElementById('arr1').textContent = nextItem1();      
		}
	);
});
	
//////////Text Array2//////////

var arr2 = [
	"Argument arr2,1",
	"Argument arr2,2",
	"Argument arr2,3",
	"Argument arr2,4",
];
	
var i2 = -1;

function nextItem2() {    
	i2 = i2 + 1;    
	i2 = i2 % arr2.length
		
  return arr2[i2];	
}

function prevItem2() {
   if (i2 === 0) { 
   i2 = arr2.length		
}
   i2 = i2 - 1; 
   return arr2[i2];	
}

window.addEventListener('load', function () {	
	document.getElementById.textContent = arr2[0]; 
	document.getElementById('prev_button').addEventListener(	
		'click', 
		function (e) { 
			document.getElementById('arr2').textContent = prevItem2();			
	}
);
    
document.getElementById('next_button').addEventListener(
		'click', 
		function (e) { 
			document.getElementById('arr2').textContent = nextItem2();				
		}
	);
});
	
//////////Text Array3//////////
	
var arr3 = [
	"Argument arr3,1",
	"Argument arr3,2",
	"Argument arr3,3",
	"Argument arr3,4",
];
	
var i3 = -1;

function nextItem3() {    
	i3 = i3 + 1;    
	i3 = i3 % arr3.length
		
  return arr3[i3];	
}

function prevItem3() {
  if (i3 === 0) { 
  i3 = arr3.length		
}
  i3 = i3 - 1; 
  return arr3[i3];	
}

window.addEventListener('load', function () {	
	document.getElementById.textContent = arr3[0]; 	
	document.getElementById('prev_button').addEventListener(	
		'click', 
		function (e) { 
			document.getElementById('arr3').textContent = prevItem3();			
	}
);
    
document.getElementById('next_button').addEventListener(
		'click', 
		function (e) { 
			document.getElementById('arr3').textContent = nextItem3();			
		}
	);
});
  
#arr1 {
	font-family:Arial,sans-serif; 
	font-size: 1em; 
	color:black;
	margin-top: 5px;
	margin-left: 10px;
}

#arr2 {
	font-family:Arial,sans-serif; 
	font-size: 1em; 
	color:black;
	margin-top: 5px;
	margin-left: 10px;
	}

#arr3 {
	font-family:Arial,sans-serif; 
	font-size: 1em; 
	color:black;
	margin-top: 5px;
	margin-left: 10px;
	}
<p id="arr1">Test Text</p>
<p id="arr2">Test Text</p>
<p id="arr3">Test Text</p>

<input type="button" id="prev_button" value="<< Prev" onclick="prev_button">
<input type="button" id="next_button" value="Next >>" onclick="next_button">

最佳答案

我不得不重构代码。帮我一个忙,在学习的同时继续阅读教程、观看视频。某些事情,例如 i3 = i3 + 1; i3 = i3 % arr3.length而不是使用索引对我来说看起来很奇怪。

// You can simplify this code alot, since basically you have exactly the same event multiple times.
// a single variable to hold the index of our active array.
var currentArray = 0;
// We can put all our arrays into an array as well, so that we can add or remove arrays without actually having to write `var arr4 = [];`
var arrays = [
	[ "Argument arr1,1", "Argument arr1,2", "Argument arr1,3", "Argument arr1,4", ],
	[ "Argument arr2,1", "Argument arr2,2", "Argument arr2,3", "Argument arr2,4", ],
	[ "Argument arr3,1", "Argument arr3,2", "Argument arr3,3", "Argument arr3,4", ]
];
// TO change the text, we need the text from 1 array to fill up all 3 of the fields.
var renderArray = function renderArray( ary ) {
	document.querySelector( '#arr1' ).textContent = ary[ 0 ];
	document.querySelector( '#arr2' ).textContent = ary[ 1 ];
	document.querySelector( '#arr3' ).textContent = ary[ 2 ];
};
// Since our active array is just an index, add or subtract 1 from the index, resetting it if the new number doesn't have an array.
var previous = function previous( event ) {
	currentArray -= 1;
	if ( currentArray < 0 ) currentArray = arrays.length - 1;
	renderArray( arrays[ currentArray ] );
};
// The reverse of previous
var next = function next( event ) {
	currentArray += 1;
	if ( currentArray > arrays.length - 1 ) currentArray = 0;
	renderArray( arrays[ currentArray ] );
};
document.querySelector( '#prev_button' ).addEventListener( 'click', previous );
document.querySelector( '#next_button' ).addEventListener( 'click', next );
<p id="arr1">Test Text</p>
<p id="arr2">Test Text</p>
<p id="arr3">Test Text</p>
<input type="button" id="prev_button" value="<< Prev">
<input type="button" id="next_button" value="Next >>">

关于javascript - 如何显示每个数组的动态文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47942907/

相关文章:

javascript - 如何使用 javascript 验证多个文本区域

javascript - 用动画从左到右浮动图像?

javascript - 如何拦截和修改来自 Chrome 扩展程序的 HTTP 身份验证请求?

javascript - 调整多个 Android 平板电脑屏幕的大小不起作用

javascript - 这个我到现在都没见过的 JavaScript 语法,它到底做了什么?

javascript - AngularJS ng-repeat 按 JSON 中的特定 ID 进行过滤

javascript - Laravel 和 Vue : where to put js/app. js?

javascript - 如何通过JavaScript重定向到404错误页面?

javascript - for循环中的点击事件

javascript - 事件处理程序中的 Edge Animate Javascript 错误