【ife】任务二十一:基础JavaScript练习(四)

2017-01-03 19:16:20来源:作者:人点击

document.getElementById("tag").onkeypress = function(e) {	var ev = e || window.event;	var key = ev.keyCode || ev.which || ev.charCode; 	switch(key) { 		case 32: 		case 13: 		case 44:		{  			var input = document.getElementById("tag").value;			var tabs = input.split(/[/s/n/r,,]/); 			for (var i = 0; i < tabs.length; i++) {				if (tabs[i] == "" || typeof(tabs[i]) == "undefined") {					tabs.splice(i, 1);					i--;				}			}			var norepeatTabs = norepeat(tabs); 			if (norepeatTabs.length > 10)				norepeatTabs.splice(0, norepeatTabs.length - 10);			show(norepeatTabs);		}	}};function norepeat(contents) {	var norepeatContents = [];	for (var i = 0; i < contents.length; i++) {		if(norepeatContents.indexOf(contents[i]) == -1)			norepeatContents.push(contents[i]);	}	return norepeatContents;}function show(tabs) { 	var content = "";	for (var i = 0; i < tabs.length; i++)		content += "<div class='tags' onmouseover='mouseover(this)' onmouseout='mouseout(this)' onclick='deleteTags(this)'>"+ tabs[i] + "</div>";	document.getElementById("tags").innerHTML = content;}function mouseover(obj) {	obj.innerHTML = "删除" + obj.innerHTML;	obj.style.background = "blue";}function mouseout(obj) { 	obj.innerHTML = obj.innerHTML.slice(2);	obj.style.background = "red";}function deleteTags(obj) { 	document.getElementById("tags").removeChild(obj);}function showHobbies() {	var hobby = document.getElementsByTagName("textarea")[0].value; 	var hobbies = hobby.split(/[/s/n/r,,、/t]/); 	for (var i = 0; i < hobbies.length; i++) { 		if (hobbies[i] == "" || typeof(hobbies[i]) == "undefined") {			hobbies.splice(i, 1);			i--;		}	}	var norepeatHobbies = norepeat(hobbies); 	if (norepeatHobbies.length > 10) 		norepeatHobbies.splice(0, norepeatHobbies.length - 10);	var content = "";	for (var i = 0; i < hobbies.length; i++) 		content += "<div class='tags''>"+ hobbies[i] + "</div>";	document.getElementById("hobbies").innerHTML = content;}

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台