Javascript代码函数解释
Javascript code Function Explanation
场景是我有一个下拉框,如下所示
<td>Select Country:</td>
<td><select id="bcountry" name="bcountry"></select>
<script language="javascript">print_country("bcountry")</script></td>
我的 JavaScript 文件中有一系列国家/地区
var country_arr = new Array("Afghanistan", "Albania",....,"n);
现在下面的代码从 HTML 调用这个函数,我真的不明白下面的函数是如何工作的
function print_country(country_id)
{
// given the id of the <select> tag as function argument, it inserts <option> tags
var option_str = document.getElementById(country_id);
option_str.length=0;
option_str.options[0] = new Option('Select Country','');
option_str.selectedIndex = 0;
for (var i=0; i<country_arr.length; i++)
{
option_str.options[option_str.length] = new Option(country_arr[i],country_arr[i]);
}
}
任何人请一步一步地向我解释上述功能print_country
感谢
为了讨论,我添加了一些行号:
function print_country(country_id)
{
// given the id of the <select> tag as function argument, it inserts <option> tags
1. var option_str = document.getElementById(country_id);
2. option_str.length=0;
3. option_str.options[0] = new Option('Select Country','');
4. option_str.selectedIndex = 0;
5. for (var i=0; i<country_arr.length; i++)
6. {
7. option_str.options[option_str.length] = new Option(country_arr[i],country_arr[i]);
8. }
}
第 1 行:文档对象模型 (DOM( 元素加载到变量option_str
第 3 行:创建一个新的 Option("label","value")
对象并将其分配给索引为 0 (0( 处option_str
的options
数组。
第 4 行:所选索引设置为零 (0(,这是 options
数组中的第一个元素。
第 5 行:您从零 (0( 循环到 country_arr
的长度,每次都递增i
。
第 7 行:创建一个新的 Option("label","value")
对象并将其分配给option_str
的最后一个位置的options
数组。Option
对象包含标签和值的country_arr
的第i
个元素。
希望能清除它!
- 创建option_str,将其指向country_id
- 向其添加标题("选择国家/地区"(
- 然后循环访问country_arr数组,并将每个国家/地区添加到option_str控件(即 country_id(。
这是函数print_country
的分解解释。
function print_country(country_id)
{
它表示print_country
的功能块的开始。
var option_str = document.getElementById(country_id);
此语句仅给出由 country_id
表示的 DOM 元素。
option_str.length=0;
option_str
的长度设置为 0。
option_str.options[0] = new Option('Select Country','');
option_str
中的第一项被赋予一个 HTML option
元素,其显示值为 Select Country
,实际值为空字符串。
option_str.selectedIndex = 0;
它将默认值设置为第一个选项值,即"选择国家/地区"。
for (var i=0; i<country_arr.length; i++)
它是数组country_arr
上的for
循环。
{
option_str.options[option_str.length] = new Option(country_arr[i],country_arr[i]);
现在,在第一个循环中,option_str
的长度为 1,因为它有一个元素Select Country
。因此,option_str
在country_arr
的循环数组上分配了 HTML option
元素。
}
}
希望它澄清。
country_id
是选择框的 ID。此函数检索此语句中的对象:
var option_str = document.getElementById(country_id);
长度最初初始化为 0,默认值也设置在此处:
option_str.length=0;
option_str.options[0] = new Option('Select Country','');
通过包含默认选项,长度将增加 1。然后在国家/地区的循环选项内设置,直到它到达country_arr
长度的末尾。
不知道从哪里调用此函数,以下是该函数正在执行的操作:
function print_country(country_id)
//function name with the argument "country_id".
//Wherever this function is being called it is calling it as
//"print_country('bcountry');
//the "bcountry" in the function call is found in the id of the <select /> tag in the HTML
{
// given the id of the <select> tag as function argument, it inserts <option> tags
var option_str = document.getElementById(country_id);
//"option_str" is now the name of the <select /> object
option_str.length=0;
//setting the option_str length to 0 eliminates the <option /> tags that are normally
//a part of the <select />, essentially clearing the drawing board to avoid duplications
option_str.options[0] = new Option('Select Country','');
//this command creates the first select <option/> giving the user the instruction to Select a country
option_str.selectedIndex = 0;
//now the selected <option />, the one that will appear in the <select> window is the instruction that was just created
for (var i=0; i<country_arr.length; i++)
{
option_str.options[option_str.length] = new Option(country_arr[i],country_arr[i]);
}
//the "for" loop, above, cycles through the array and for each member of the Array it creates a new <option> for the <select> tag
}
您的代码:
- 创建一个空的
<select>
下拉列表 - 然后,脚本根据 ID 引用下拉列表 ,由
- 该脚本将一个带有标签"选择国家/地区"的选项(占位符(作为第一个选项,并将空字符串作为值。
- 然后,它为
country_arr
数组中的每个项目追加一个<option>
。它们中的每一个都对其标签和值使用相同的值。
country_id
我在你的代码中添加了注释。
function print_country(country_id) // this is function declaration, have one parameter : country_id
{
// given the id of the <select> tag as function argument, it inserts <option> tags
var option_str = document.getElementById(country_id); // Get the element in your html with the id equals to country_id, the element should be a dropdown list
option_str.length=0 // clear the dropdown list item
option_str.options[0] = new Option('Select Country',''); // add first item in dropdown list
option_str.selectedIndex = 0; // set selected item to dropdown list
for (var i=0; i<country_arr.length; i++) // loop for the array
{
option_str.options[option_str.length] = new Option(country_arr[i],country_arr[i]); // create new item in your dropdown list
}
}
更多:Javascript Select
将 selectbox html 对象分配给变量 option_str。
var option_str = document.getElementById(country_id);
将选项数设置为 0。(万一我猜 html 中有(
option_str.length=0;
将第一个选项的值设置为"选择国家/地区">
option_str.options[0] = new Option('Select Country','');
将默认选定选项设置为第一个选项。
option_str.selectedIndex = 0;
遍历country_arr数组。
for (var i=0; i<country_arr.length; i++)
以下代码将对country_arry中的每个项执行一次。
在 javascript 中,您可以使用以下语法将任何项添加到任何索引处的数组中:
myArray[index] = item
确保插入发生在数组的末尾。
option_str.options[option_str.length]
由于 javascript 中的数组是从 0 开始的,并且 length 属性从 1 开始,因此使用 option_str.length 作为索引可确保将其插入数组的末尾。
插入新选项,其中选项文本是country_arr中的当前项并且选项值也是country_arry中的当前项目
new Option(country_arr[i],country_arr[i])
- 你能解释一下这个阶乘函数是如何工作的吗?
- getDataAsJSON()在PHP中是一个未定义的函数,但许多解释如何使用JSONP的网站都说要使用它
- 解释用于检测vml的javascript函数
- 有人可以解释一下Object.defineProperty如何在javascript的构造函数中工作
- 有人可以详细解释这个 JavaScript 十进制舍入函数吗?
- 无法解释的未捕获类型错误:未定义不是函数
- 有人能向我解释一下为什么我们在这个函数中需要一个for循环吗
- 请解释一下“;立即调用函数表达式“;以及“;自调用匿名函数”;
- 你能解释一下这种奇怪的函数声明行为吗?
- 请解释有关 JavaScript 中的原型属性和函数构造函数的详细信息
- 谁能解释为什么我的日期函数通过 JS 日期对象给了我错误的转换
- 你能解释一下jQuery如何同时使用$作为函数和对象吗?
- 解释在 Parse 中执行 find 函数 inisde for 循环背后的逻辑
- 有人可以用初学者术语解释一下这个序数函数是如何工作的
- 任何人都可以解释JavaScript中的reduce函数吗?
- 一些帮助解释javascript函数
- 谁能解释一下这个 ajax .each 函数中的项目来自哪里
- 需要解释函数表达式如何在函数声明中作为参数传递
- 解释函数的作用
- 有人能向我解释函数.protype.call()和函数.protype=对象.create()吗