没有访问函数中推送的数组;在JavaScript中的函数外部使用Onclick

huangapple go评论84阅读模式
英文:

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:

&lt;body&gt;
    &lt;input type=&quot;button&quot; value=&quot;click me&quot; onclick=&quot;you()&quot;&gt;

    &lt;script&gt;
        var me = [4];

        function you() {
            me.push(2, 3);
            console.log(&#39;inside = &#39; + me)

            //Displayed value in console after click: inside = 4,2,3

        }
        console.log(&#39;outside = &#39; + me);

        //Displayed value in console after and before click: outside = 4

    &lt;/script&gt;
&lt;/body&gt;

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:

&lt;body&gt;

    &lt;script&gt;
        var me = [4];

        function you() {
            me.push(2, 3);
            console.log(&#39;inside = &#39; + me)

            //Displayed value in console: inside = 4,2,3

        }

        you();
        console.log(&#39;outside = &#39; + me);

        //Displayed value in console: outside = 4,2,3

    &lt;/script&gt;
&lt;/body&gt;

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.logyou()函数被调用之前运行。因此它无法显示更新,因为更新尚未发生。

你需要稍后运行一个函数来查看最新的更新。

在下面的测试示例中,如果你首先点击“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(&#39;inside = &#39; + me)
}

function seeUpdates(){
  console.log(&#39;outside = &#39; + me);
}

<!-- language: lang-html -->

&lt;input type=&quot;button&quot; value=&quot;click me&quot; onclick=&quot;you()&quot;&gt;
&lt;input type=&quot;button&quot; value=&quot;See Updates&quot; onclick=&quot;seeUpdates()&quot;&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月15日 01:24:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定