JavaScript in 10 Simple Steps or Less 2007 phần 2

yêu cầu 21 đã giới thiệu cho các bạn làm thế nào để tạo ra một mảng. Một mảng là không phải là rất hữu ích, tuy nhiên, trừ khi bạn có thể cư các yếu tố của nó với các giá trị. Bạn cư các yếu tố của một mảng bằng cách gán giá trị cho các yếu tố cũng giống như bạn gán giá trị cho biến bình thường: | JavaScript Basics 41 The following task creates an array in a script in the header of a document 1. Open a new HTML document in your preferred HTML or text editor. 2. Create the head of the document with opening and closing head tags head Task 20 head 3. Insert a script block in the head of the document head script language JavaScript -- -- script head 4. Create a variable named myArray and initialize it as a new array with five elements body script language JavaScript -- var myArray new Array 5 -- script body 5. Save the file and close it. 42 Part 1 Task Populating an Array Task 21 showed you how to create an array. An array isn t very useful however unless you can populate its elements with values. You populate the elements of an array by assigning values to the elements just as you assign values to normal variables note The numeric value for each container in an array is known as the index. arrayName 0 value 1 arrayName 1 value 2 etc. In addition you can actually populate the array at the time you create it instead of specifying the number of elements to create in the array when you create it you can specify a comma-separated list of values for the elements of the array var arrayName new Array value 1 value 2 value 3 etc. The following task illustrates the creation of two arrays that will contain an identical set of five elements. The two arrays are created and populated using these two different techniques. 1. Open a new HTML document in your preferred HTML or text editor. 2. Create the head of the document with opening and closing head tags head head 3. Insert a script block in the head of the document head script language JavaScript -- -- script head 4. Create a variable named myArray and initialize it as a new array with five elements var myArray new Array 5 JavaScript Basics 43 5. Assign values to the five elements myArray 0 myArray 1 myArray 2 myArray 3 myArray 4 First Entry Second Entry Third Entry Fourth Entry Fifth Entry Task 6. Create a second

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
34    92    2    30-04-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.