How to pass what a function calculates as a variable
天蓬老师
天蓬老师 2017-06-26 10:55:07
0
2
867

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

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(2)
phpcn_u1582

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

小葫芦

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template