数组some方法
发布时间:2025-05-24 13:59:48 发布人:远客网络
一、数组some方法
1、数组的some方法,数组array是javascript里面一种数据的存储方式,而数组里面有许多方法,some就是其中一种方法,用于判断获取数据的时候什么时候进行终止循环,对于大型数据有着分割的能力。
2、windows 7 sublime text 3 chrome浏览器
3、打开编辑器,新建一个HTML文档,作为示范,然后设立HTML的基本架构。
4、创建JS文件,并且关联HTML文档。
5、"Peter","Alice","Chris","Baby","Sherry"];
6、let test1= students.some(function(title, index){
7、创建一个数组,并且使用some方法。
8、some遇到true以后就停止循环了,因此只打印了第一个元素。
9、let test1= students.some(function(title, index){
10、如果是遇到false就会一直循环下去,知道打印完。
11、return title.indexOf("Alice")>-1;
12、改变一下判断条件,遇到"Alice"以后再停止循环。
13、let test1= students.some(function(title, index){
14、我们还可以用if语句来进行判断,这样可以终止循环。
二、js使用childNodes取值问题
1、childNodes这个方法基本没法用,存在严重的浏览器兼容性问题,里面可能包含了很多没用的空节点,所以你用childNodes(索引)获取元素是不对的。
2、table中获取tr、td等还是挺方便的像这样:
3、如果要获取td里面没有id的元素比如input那只能用这个方法:
4、var list=tbs.rows[索引].cells[索引].getElementsByTagName('input');
5、普通应用下你可以当js没有childNodes这个方法
三、JS自带有几种方法
1:<script language=”JavaScript”>
1:<script language=”JavaScript”>
在不支持JavaScript的浏览器中将不执行相关代码
2: Hello to the non-JavaScript browser.
1:<script language=”JavaScript” src="”filename.js"”></script>
2: document.write(“Hello”);// This is a comment
1: document.write(“<strong>Hello</strong>”);
1: var myVariable=“some value”;
1: var myString=“String1”+“String2”;
1:<script language=”JavaScript”>
3: var myVariable=“Hello there”;
4: var therePlace= myVariable.search(“there”);
5: document.write(therePlace);
1: thisVar.replace(“Monday”,”Friday”);
1:<script language=”JavaScript”>
3: var myVariable=“Hello there”;
4: document.write(myVariable.big()+“<br>”);
5: document.write(myVariable.blink()+“<br>”);
6: document.write(myVariable.bold()+“<br>”);
7: document.write(myVariable.fixed()+“<br>”);
8: document.write(myVariable.fontcolor(“red”)+“<br>”);
9: document.write(myVariable.fontsize(“18pt”)+“<br>”);
10: document.write(myVariable.italics()+“<br>”);
11: document.write(myVariable.small()+“<br>”);
12: document.write(myVariable.strike()+“<br>”);
13: document.write(myVariable.sub()+“<br>”);
14: document.write(myVariable.sup()+“<br>”);
15: document.write(myVariable.toLowerCase()+“<br>”);
16: document.write(myVariable.toUpperCase()+“<br>”);
18: var firstString=“My String”;
19: var finalString= firstString.bold().toLowerCase().fontcolor(“red”);
1:<script language=”JavaScript”>
4: myArray[0]=“First Entry”;
5: myArray[1]=“Second Entry”;
6: myArray[2]=“Third Entry”;
7: myArray[3]=“Fourth Entry”;
8: myArray[4]=“Fifth Entry”;
9: var anotherArray= new Array(“First Entry”,”Second Entry”,”Third Entry”,”Fourth Entry”,”Fifth Entry”);
1:<script language=”JavaScript”>
9: document.write(myArray.sort());
1:<script language=”JavaScript”>
3: var myVariable=“a,b,c,d”;
4: var stringArray= myVariable.split(“,”);
5: document.write(stringArray[0]);
6: document.write(stringArray[1]);
7: document.write(stringArray[2]);
8: document.write(stringArray[3]);
1:<script language=”JavaScript”>
1:<script language=”JavaScript”>
3: var result= window.confirm(“Click OK to continue”);
1:<script language=”JavaScript”>
3: function multiple(number1,number2){
4: var result= number1* number2;
1:<a href=”#” onClick=”functionName()”>Link text</a>
2:<a href="/”javascript:functionName"()”>Link text</a>
1:<body onLoad=”functionName();”>
3: var userChoice= window.confirm(“Choose OK or Cancel”);
4: var result=(userChoice== true)?“OK”:“Cancel”;
7: for(i= 0; i< myArray.length; i++){
8: document.write(myArray[i]+“<br>”);
6: window.setTimeout(“hello()”,5000);
5: window.setTimeout(“hello()”,5000);
7: window.setTimeout(“hello()”,5000);
6: var myTimeout= window.setTimeout(“hello()”,5000);
7: window.clearTimeout(myTimeout);
1:<body onUnload=”functionName();”>
JavaScript就这么回事2:浏览器输出
1:<script language=”JavaScript”>
1:<script language=”JavaScript”>
2: document.write(“<p>Here’s some information about this document:</p>”);
3: document.write(“<ul>”);
4: document.write(“<li>Referring Document:“+ document.referrer+“</li>”);
5: document.write(“<li>Domain:“+ document.domain+“</li>”);
6: document.write(“<li>URL:“+ document.URL+“</li>”);
7: document.write(“</ul>”);
1: document.writeln(“<strong>a</strong>”);
1:<script language=”JavaScript”>
3: document.write(thisDate.toString());
1:<script language=”JavaScript”>
3: var currentDate= new Date();
4: var userOffset= currentDate.getTimezoneOffset()/60;
5: var timeZoneDifference= userOffset- myOffset;
6: currentDate.setHours(currentDate.getHours()+ timeZoneDifference);
7: document.write(“The time and date in Central Europe is:“+ currentDate.toLocaleString());
1:<script language=”JavaScript”>
3: var thisTimeString= thisDate.getHours()+“:”+ thisDate.getMinutes();
4: var thisDateString= thisDate.getFullYear()+“/”+ thisDate.getMonth()+“/”+ thisDate.getDate();
5: document.write(thisTimeString+“ on“+ thisDateString);
1:<script language=”JavaScript”>
2: var urlParts= document.URL.split(“?”);
3: var parameterParts= urlParts[1].split(“&”);
4: for(i= 0; i< parameterParts.length; i++){
5: var pairParts= parameterParts[i].split(“=”);
6: var pairName= pairParts[0];
7: var pairValue= pairParts[1];
8: document.write(pairName+“:“+pairValue);
1:<script language=”JavaScript”>
4: document.write(“<p>This is a New Document.</p>”);
1:<script language=”JavaScript”>
2: window.location=“”;
3:<script language='javaScript'>
4: var placeHolder= window.open('holder.html','placeholder','width=200,height=200');
6:<title>The Main Page</title>
8:<body onLoad='placeHolder.close()'>
9:<p>This is the main page</p>
1:<img src="”image1.jpg"” name=”myImage”>
2:<a href=”#” onClick=”window.alert(document.myImage.width)”>Width</a>
1:<script language=”JavaScript”>
3: myImage.src=“Tellers1.jpg”;
1:<script language=”JavaScript”>
3: rollImage.src=“rollImage1.jpg”;
5: defaultImage.src=“image1.jpg”;
7:<a href="/”myUrl"” onMouseOver=”document.myImage.src= rollImage.src;”
8: onMouseOut=”document.myImage.src= defaultImage.src;”>
9:<img src="”image1.jpg"” name=”myImage” width=100 height=100 border=0>
1:<script language=”JavaScript”>
3: imageList[0]=“image1.jpg”;
4: imageList[1]=“image2.jpg”;
5: imageList[2]=“image3.jpg”;
6: imageList[3]=“image4.jpg”;
7: var imageChoice= Math.floor(Math.random()* imageList.length);
8: document.write(‘<img src=”’+ imageList[imageChoice]+‘“>’);
1:<script language=”JavaScript”>
4: function createRollOver(originalImage,replacementImage){
6: imageArray[source]= new Image;
7: imageArray[source].src= originalImage;
8: imageArray[replacement]= new Image;
9: imageArray[replacement].src= replacementImage;
12: var rollImage1= createRollOver(“image1.jpg”,”rollImage1.jpg”);
14:<a href=”#” onMouseOver=”document.myImage1.src= rollImage1[replacement].src;”
15: onMouseOut=”document.myImage1.src= rollImage1[source].src;”>
16:<img src="”image1.jpg"” width=100 name=”myImage1” border=0>
1:<script language=”JavaScript”>
4: imageList[0].src=“image1.jpg”;
6: imageList[1].src=“image2.jpg”;
8: imageList[2].src=“image3.jpg”;
10: imageList[3].src=“image4.jpg”;
11: function slideShow(imageNumber){
12: document.slideShow.src= imageList[imageNumber].src;
14: if(imageNumber< imageList.length){
15: window.setTimeout(“slideShow(“+ imageNumber+“)”,3000);
20:<body onLoad=”slideShow(0)”>
21:<img src="”image1.jpg"” width=100 name=”slideShow”>
1:<script language=”JavaScript”>
3: imageList[0]=“image1.jpg”;
4: imageList[1]=“image2.jpg”;
5: imageList[2]=“image3.jpg”;
6: imageList[3]=“image4.jpg”;
8: urlList[0]=“”;
9: urlList[1]=“”;
10: urlList[2]=“”;
11: urlList[3]=“”;
12: var imageChoice= Math.floor(Math.random()* imageList.length);
13: document.write(‘<a href=”’+ urlList[imageChoice]+‘“><img src=”’+ imageList[imageChoice]+‘“></a>’);
还是先继续写完JS就这么回事系列吧~
1:<form method=”post” action=”target.html” name=”thisForm”>
2:<input type=”text” name=”myText”>
3:<select name=”mySelect”>
4:<option value=”1”>First Choice</option>
5:<option value=”2”>Second Choice</option>
8:<input type=”submit” value=”Submit Me”>
1:<form name=”myForm”>
2:<input type=”text” name=”myText”>
4:<a href='#' onClick='window.alert(document.myForm.myText.value);'>Check Text Field</a>
1:<form name=”myForm”>
2: Enter some Text:<input type=”text” name=”myText”><br>
3: Copy Text:<input type=”text” name=”copyText”>
5:<a href=”#” onClick=”document.myForm.copyText.value=
6: document.myForm.myText.value;”>Copy Text Field</a>
1:<form name=”myForm”>
2: Enter some Text:<input type=”text” name=”myText” onChange=”alert(this.value);”>
1:<form name=”myForm”>
2:<select name=”mySelect”>
3:<option value=”First Choice”>1</option>
4:<option value=”Second Choice”>2</option>
5:<option value=”Third Choice”>3</option>
8:<a href='#' onClick='alert(document.myForm.mySelect.value);'>Check Selection List</a>
1:<form name=”myForm”>
2:<select name=”mySelect”>
3:<option value=”First Choice”>1</option>
4:<option value=”Second Choice”>2</option>
7:<script language=”JavaScript”>
8: document.myForm.mySelect.length++;
9: document.myForm.mySelect.options[document.myForm.mySelect.length- 1].text=“3”;
10: document.myForm.mySelect.options[document.myForm.mySelect.length- 1].value=“Third Choice”;
1:<script language=”JavaScript”>
2: function checkField(field){
4: window.alert(“You must enter a value in the field”);
9:<form name=”myForm” action=”target.html”>
10: Text Field:<input type=”text” name=”myField”onBlur=”checkField(this)”>
11:<br><input type=”submit”>
1: function checkList(selection){
3: window.alert(“You must make a selection from the list.”);
1:<form name=”myForm” action=”login.html”>
2: Username:<input type=”text” name=”username”><br>
3: Password:<input type=”password” name=”password”><br>
4:<input type=”button” value=”Login” onClick=”this.form.submit();”>
5:<input type=”button” value=”Register” onClick=”this.form.action=‘register.html’; this.form.submit();”>
6:<input type=”button” value=”Retrieve Password” onClick=”this.form.action=‘password.html’; this.form.submit();”>
1:<form name=”myForm” action=”login.html”>
2: Username:<input type=”text” name=”username”><br>
3: Password:<input type=”password”name=”password”><br>
4:<input type=”image” src="”login.gif"” value=”Login”>
1:<SCRIPT LANGUAGE='JavaScript'>
5: for(i=0; i< item.length; i++){
6: newItem+= item.charCodeAt(i)+'.';
10: function encryptForm(myForm){
11: for(i=0; i< myForm.elements.length; i++){
12: myForm.elements[i].value= encrypt(myForm.elements[i].value);
18:<form name='myForm' onSubmit='encryptForm(this); window.alert(this.myField.value);'>
19: Enter Some Text:<input type=text name=myField><input type=submit>
JavaScript就这么回事5:窗口和框架
1:<script language=”JavaScript”>
2: window.status=“A new status message”;
1:<script language=”JavaScript”>
2: var userChoice= window.confirm(“Click OK or Cancel”);
4: document.write(“You chose OK”);
6: document.write(“You chose Cancel”);
1:<script language=”JavaScript”>
2: var userName= window.prompt(“Please Enter Your Name”,”Enter Your Name Here”);
3: document.write(“Your Name is“+ userName);
1://打开一个名称为myNewWindow的浏览器新窗口
2:<script language=”JavaScript”>
3: window.open(“”,”myNewWindow”);
1:<script language=”JavaScript”>
2: window.open(“”,”myNewWindow”,'height=300,width=300');
1:<script language=”JavaScript”>
2: window.open(“”,”myNewWindow”,'height=300,width=300,left=200,screenX=200,top=100,screenY=100');
1:<script language=”JavaScript”>
1:<script language=”JavaScript”>
2: window.open(';);</script>
1:<a href='#' onClick='document.location='125a.html';'>Open New Document</a>
1:<script language=”JavaScript”>
2: if(document.all){//如果是IE浏览器则使用scrollTop属性
3: document.body.scrollTop= 200;
4:} else{//如果是NetScape浏览器则使用pageYOffset属性
1:<a href='#' onClick=”window.open(';);”>Open a full-screen window</a>
1:<script language=”JavaScript”>
3: var newWindow= window.open(“128a.html”,”newWindow”);
4: newWindow.close();//在父窗口中关闭打开的新窗口
1:<script language=”JavaScript”>
2: var newWindow= window.open(“”,”newWindow”);
4: newWindow.document.write(“This is a new window”);
5: newWIndow.document.close();
1:<frameset cols=”50%,*”>
2:<frame name=”frame1” src="”135a.html"”>
3:<frame name=”frame2” src="”about:blank"”>
5:在frame1中加载frame2中的页面
6: parent.frame2.document.location=“135b.html”;
如果在frame1中html文件中有个脚本
2: window.alert(“Frame 1 is loaded”);
那么在frame2中可以如此调用该方法
1:<body onLoad=”parent.frame1.doAlert();”>
可以在框架页面定义数据项,使得该数据可以被多个框架中的页面公用
1:<script language=”JavaScript”>
2: var persistentVariable=“This is a persistent value”;
4:<frameset cols=”50%,*”>
5:<frame name=”frame1” src="”138a.html"”>
6:<frame name=”frame2” src="”138b.html"”>
这样在frame1和frame2中都可以使用变量persistentVariable
根据以上的一些思路,我们可以使用一个隐藏的框架页面来作为整个框架集的代码库
1:<frameset cols=”0,50%,*”>
2:<frame name=”codeFrame” src="”140code.html"”>
3:<frame name=”frame1” src="”140a.html"”>
4:<frame name=”frame2” src="”140b.html"”>