search

Home  >  Q&A  >  body text

How to pass what a function calculates as a variable

How to pass the content calculated by the function into a variable.

<!DOCTYPE html>
<html lang="zh">

<head>
    <title>照片爬取</title>
    <meta charset="UTF-8">
</head>

<body>
    <script>
        var a = 0;
        function photo() {
            while (a < 10) {
                a = a + 1;
                var url = "http://work.ynzs.cn/ZSGL/servletphoto?path=kszp/22240205/300y00" + a + ".jpg";

                document.write(url + ",");

            }
            while (a < 100) {
                a = a + 1;
                var url = "http://work.ynzs.cn/ZSGL/servletphoto?path=kszp/22240205/300y0" + a + ".jpg";

                document.write(url + ",");

            }
        }
    </script>
    <input name="button" type="button" onClick="photo()" value="NEXT" />
</body>

</html>

Like the code, use this calculation to output the image address I want, and then use the following code to display the image on the page

   <script>
        var img = “图片地址”;

        var body = document.querySelector('body');
        var imgArr = img.split(",");
        var tem, j = 0;
        document.querySelector('#showall').addEventListener('click', showa);
        function showa() {
            for (j; j < imgArr.length; j++) {
                tem = document.createElement('img');
                tem.src = imgArr[j];
                body.appendChild(tem);
            }
        }
    </script>
    <input name="button" type="button" onClick="showa()" value="show" />

But how to pass the content calculated by the first paragraph of code to the second paragraph? ? ?
I am a JS beginner, please give me some advice

天蓬老师天蓬老师2784 days ago900

reply all(2)I'll reply

  • phpcn_u1582

    phpcn_u15822017-06-26 10:57:07

    ‍ I think it’s okay to encapsulate the second code into a function that receives the image address as a parameter

    reply
    0
  • 高洛峰

    高洛峰2017-06-26 10:57:07

    Judging from the meaning of the title, you return the calculated value inside the function, and when running this function, just use a variable to receive it.

    reply
    0
  • Cancelreply