English 中文(简体)
如何通过 javamart 更改数组元素的值?
原标题:How to change value of an array element through javascript?

我想做一个按钮来改变阵列中元素的价值。 我试图用下面的代码来做,但元素不会改变。 作为自我学习的初学者,我可能漏掉了一些非常明显的东西,如果有人能帮我指出这一点,我将不胜感激。

谢谢你的回答!

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Change Array Value</title>

</head>

<body>
<textarea id="log2"></textarea>
<input type="button" onClick="uClicked();" value="Click!">
<script>
    var fer=[];

    for (i=0; i< 15; i++){
            fer[i]=i+1;
    }

    function uClicked(fer){
        fer[12] = 10; 
        return fer[12];
    }
    log2.value = "fer[12]= " + fer[12];

</script>
</body>
</html>
最佳回答
function uClicked(){ // remove the parameter.

参数不需要, 正在隐藏真实的 < code> fer < /code > 变量 。

因为 fer 被宣布为外部范围,所以 uClicked 功能可以访问它。

<强度 > 固定代码:

var fer=[];

for (i=0; i< 15; i++){
        fer[i]=i+1;
}

function uClicked(){
    fer[12] = 10; 
    alert(fer[12]);
}
问题回答

您带有注释的代码

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Change Array Value</title>

</head>

<body>
<textarea id="log2"></textarea>
<input type="button" onClick="uClicked();" value="Click!">
<script>
var fer=[];

for (i=0; i< 15; i++){ //use var i, otherwise you are putting i in the global scope
        fer[i]=i+1;
}

function uClicked(fer){ // fer is undefined because you are not passing argument when you call the function
    fer[12] = 10; 
    return fer[12];
}
log2.value = "fer[12]= " + fer[12]; //log2 is not defined.

</script>
</body>
</html>

工作守则:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Change Array Value</title>

</head>

<body>


<textarea id="log2"></textarea>
<input type="button" onclick="uClicked();" value="Click!" />
<script type="text/javascript">
var fer = [];
for(var i; i < 15; i++){
        fer[i]=i+1;
}
var log2 = document.getElementById("log2");
function uClicked(){
    fer[12] = 10; 
    log2.value = "fer[12]= " + fer[12];
    return fer[12];
}
</script>
​


​</body>
</html>




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签