javascript函数中的参数是干什么用的?参数可以随便定义吗?
<html><head><scripttype="text/javascript">functionmyfunction(txt){alert(txt)}</script...
<html>
<head>
<script type="text/javascript">
function myfunction(txt)
{
alert(txt)
}
</script>
</head>
<body>
<form>
<input type="button"
onclick="myfunction('早安!')"
value="在早晨">
<input type="button"
onclick="myfunction('晚安!')"
value="在夜晚">
</form>
<p>通过点击这个按钮,可以调用一个函数。该函数会输出传递给它的参数。</p>
</body>
</html>
这个txt是可以随便定义的吗? 展开
<head>
<script type="text/javascript">
function myfunction(txt)
{
alert(txt)
}
</script>
</head>
<body>
<form>
<input type="button"
onclick="myfunction('早安!')"
value="在早晨">
<input type="button"
onclick="myfunction('晚安!')"
value="在夜晚">
</form>
<p>通过点击这个按钮,可以调用一个函数。该函数会输出传递给它的参数。</p>
</body>
</html>
这个txt是可以随便定义的吗? 展开
4个回答
展开全部
是的,用的时候一至就可以.但有些javascript保留字不能用,比如for, var, function, 这些...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
参数可以随便定义,只要符合明明规范就可以啦,在这里点击button的时候就会调用myfunction()方法,并将"早安"或者"晚安"传给它,然后就会输出相应的结果了!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<html>
<head>
<script type="text/javascript">
function testTypeOf()
{
var test_int = 1;
var test_string = 'second';
var test_array = new Array();
test_array[0] = 3;
test_array[1] = '3';
test_array[3] = new Object();
var test_button_dom = document.getElementById("typeOfBtn");
myfunctionTypeOf(test_int, test_string, test_array, test_button_dom);
}
function myfunctionTypeOf(test_int, test_string, test_array, test_button_dom) {
var table = document.getElementById("resultTable");
var tr = table.insertRow();
var td0 = tr.insertCell();
td0.style.color='red';
td0.innerHTML = 'typeof';
var td1 = tr.insertCell();
td1.innerHTML = typeof(test_int);
var td2 = tr.insertCell();
td2.innerHTML = typeof(test_string);
var td3 = tr.insertCell();
td3.innerHTML = typeof(test_array);
var td4 = tr.insertCell();
td4.innerHTML = typeof(test_button_dom);
}
function testValueOf()
{
var test_int = 1;
var test_string = 'second';
var test_array = new Array();
test_array[0] = 3;
test_array[1] = '3';
test_array[3] = new Object();
var test_button_dom = document.getElementById("valueOfBtn");
myfunctionValueOf(test_int, test_string, test_array, test_button_dom);
}
function myfunctionValueOf(test_int, test_string, test_array, test_button_dom) {
var table = document.getElementById("resultTable");
var tr = table.insertRow();
var td0 = tr.insertCell();
td0.style.color='red';
td0.innerHTML = 'valueof';
var td1 = tr.insertCell();
td1.innerHTML = test_int;
var td2 = tr.insertCell();
td2.innerHTML = '"' + test_string + '"';
var td3 = tr.insertCell();
td3.innerHTML = test_array;
var td4 = tr.insertCell();
td4.innerHTML = test_button_dom;
}
function doclear() {
var table = document.getElementById("resultTable");
while (table.rows.length > 1) {
table.deleteRow(table.rows.length - 1);
}
}
</script>
</head>
<body>
<input type="button" id="typeOfBtn" onclick="testTypeOf()" value="Test TypeOf">
<input type="button" id="valueOfBtn" onclick="testValueOf()" value="Test ValueOf">
<input type="button" onclick="doclear()" value="Clear">
<div style="overflow: auto">
<table id="resultTable" border="1">
<thead>
<tr>
<td> </td><td>test_int</td><td>test_string</td><td>test_array</td><td>test_button_dom</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</body>
</html>
<head>
<script type="text/javascript">
function testTypeOf()
{
var test_int = 1;
var test_string = 'second';
var test_array = new Array();
test_array[0] = 3;
test_array[1] = '3';
test_array[3] = new Object();
var test_button_dom = document.getElementById("typeOfBtn");
myfunctionTypeOf(test_int, test_string, test_array, test_button_dom);
}
function myfunctionTypeOf(test_int, test_string, test_array, test_button_dom) {
var table = document.getElementById("resultTable");
var tr = table.insertRow();
var td0 = tr.insertCell();
td0.style.color='red';
td0.innerHTML = 'typeof';
var td1 = tr.insertCell();
td1.innerHTML = typeof(test_int);
var td2 = tr.insertCell();
td2.innerHTML = typeof(test_string);
var td3 = tr.insertCell();
td3.innerHTML = typeof(test_array);
var td4 = tr.insertCell();
td4.innerHTML = typeof(test_button_dom);
}
function testValueOf()
{
var test_int = 1;
var test_string = 'second';
var test_array = new Array();
test_array[0] = 3;
test_array[1] = '3';
test_array[3] = new Object();
var test_button_dom = document.getElementById("valueOfBtn");
myfunctionValueOf(test_int, test_string, test_array, test_button_dom);
}
function myfunctionValueOf(test_int, test_string, test_array, test_button_dom) {
var table = document.getElementById("resultTable");
var tr = table.insertRow();
var td0 = tr.insertCell();
td0.style.color='red';
td0.innerHTML = 'valueof';
var td1 = tr.insertCell();
td1.innerHTML = test_int;
var td2 = tr.insertCell();
td2.innerHTML = '"' + test_string + '"';
var td3 = tr.insertCell();
td3.innerHTML = test_array;
var td4 = tr.insertCell();
td4.innerHTML = test_button_dom;
}
function doclear() {
var table = document.getElementById("resultTable");
while (table.rows.length > 1) {
table.deleteRow(table.rows.length - 1);
}
}
</script>
</head>
<body>
<input type="button" id="typeOfBtn" onclick="testTypeOf()" value="Test TypeOf">
<input type="button" id="valueOfBtn" onclick="testValueOf()" value="Test ValueOf">
<input type="button" onclick="doclear()" value="Clear">
<div style="overflow: auto">
<table id="resultTable" border="1">
<thead>
<tr>
<td> </td><td>test_int</td><td>test_string</td><td>test_array</td><td>test_button_dom</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询