英文:
No access to the array pushed in the function; Outside the function with Onclick in JavaScript
问题
我在函数外部定义了一个数组,然后定义了一个函数,在函数内部将值推入数组。
问题在于,如果我使用Onclick调用函数,我无法在函数外部访问我在函数内推入的值:
<body>
<input type="button" value="click me" onclick="you()">
<script>
var me = [4];
function you() {
me.push(2, 3);
console.log('inside = ' + me)
// 点击后控制台显示的值: inside = 4,2,3
}
console.log('outside = ' + me);
// 点击前后控制台显示的值: outside = 4
</script>
</body>
但如果我在JavaScript内部调用相同的函数,那么在函数外部,我也能够访问推入函数的值:
<body>
<script>
var me = [4];
function you() {
me.push(2, 3);
console.log('inside = ' + me)
// 控制台显示的值: inside = 4,2,3
}
you();
console.log('outside = ' + me);
// 控制台显示的值: outside = 4,2,3
</script>
</body>
如何在函数外部访问通过Onclick推入函数的值?
我尝试了与上述方法相同的方式,但是即使搜索也没有找到答案;如果你能帮助我,我将非常高兴。
英文:
I defined an array outside the function, then defined a function and inside the function pushed values into the array.
The problem is that if I call the function with Onclick. I don't have access outside the function to the values I pushed inside the function:
<body>
<input type="button" value="click me" onclick="you()">
<script>
var me = [4];
function you() {
me.push(2, 3);
console.log('inside = ' + me)
//Displayed value in console after click: inside = 4,2,3
}
console.log('outside = ' + me);
//Displayed value in console after and before click: outside = 4
</script>
</body>
But if I call the same function inside JavaScript; In that case, outside of the function as well, I will have access to the values pushed into the function:
<body>
<script>
var me = [4];
function you() {
me.push(2, 3);
console.log('inside = ' + me)
//Displayed value in console: inside = 4,2,3
}
you();
console.log('outside = ' + me);
//Displayed value in console: outside = 4,2,3
</script>
</body>
How can I access outside the function the values that I pushed inside the function by Onclick?
I tried the same method as above, but I did not find an answer even by searching; I would be very happy if you could help me.
答案1
得分: 0
在你的第一个例子中,你的console.log
在you()
函数被调用之前运行。因此它无法显示更新,因为更新尚未发生。
你需要稍后运行一个函数来查看最新的更新。
在下面的测试示例中,如果你首先点击“click me”按钮,数组会被更新。然后点击第二个按钮,你将看到更新后的数组。
<input type="button" value="click me" onclick="you()">
<input type="button" value="See Updates" onclick="seeUpdates()">
英文:
In your first example, your console.log is ran BEFORE the you() function is called. So it can't show the updates as they haven't happened yet.
You will need to run a function later on to see the newest updates.
In the below test example, if you first click the click me button, the array is updated. Then clicking the second button you will see the updated array.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var me = [4];
function you() {
me.push(2, 3);
console.log('inside = ' + me)
}
function seeUpdates(){
console.log('outside = ' + me);
}
<!-- language: lang-html -->
<input type="button" value="click me" onclick="you()">
<input type="button" value="See Updates" onclick="seeUpdates()">
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论