javascript 如何在Javascript中打印出一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17579311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:51:17  来源:igfitidea点击:

How to print out line in Javascript

javascript

提问by user2514364

I want to test the return value of this function getFileExtension(input.files[0].name) (I have a comment there to point at that line) My question is how to print out that value in javascript? Thanks!

我想测试这个函数的返回值 getFileExtension(input.files[0].name) (我在那里有一条评论指向那一行)我的问题是如何在 javascript 中打印出该值?谢谢!

<script>
    function getFileExtension(filename) {
        var ext=filename.split('.').pop();
        return ext
    }
</script>

<input type='file' onchange="readURL(this);"> 

<script>
function readURL(input) {
    if (input.files && input.files[0]) {
        if (getFileExtension(input.files[0].name)=="png") { // this line is our problem 
            var reader = new FileReader();
            reader.onload = function (e) {
                document.getElementById("pdf").innerHTML="<img id='blah' src=" +
                                       e.target.result + " alt='your image' width='450'>"
            }
            reader.readAsDataURL(input.files[0]);
        }
    }
}
</script>

回答by Peter Rasmussen

You can print out values in javascript by writing

您可以通过编写在 javascript 中打印出值

console.log(value);

Most browsers have a console which you can see by pressing f12. The values will end up there.

大多数浏览器都有一个控制台,您可以通过按 来查看f12。值将在那里结束。

If you are using Internet explorer remember to have developer tools open (f12) or you will get an error.

如果您使用的是 Internet Explorer,请记住打开开发人员工具 (f12),否则您将收到错误消息。

回答by Stephen P

var msg = 'value: ' + yourVar;
if (console) {
    console.log(msg); // or console.info(msg) or whatever
} else {
    alert(msg);
}

Personally, I wrote a simple lightweight console so I can output to any div on a page. I did that instead of a lot of document.writeas Avitus suggests in a comment.

就个人而言,我编写了一个简单的轻量级控制台,以便可以输出到页面上的任何 div。我这样做了,而不是document.write像 Avitus 在评论中建议的那样做很多。

回答by Alexandru Nedelcu

I would use Firebug plugin for Firefox for more detailed error descriptions and in order to see the values plugged into console.log(value)

我会使用 Firefox 的 Firebug 插件来获取更详细的错误描述,并查看插入到 console.log(value) 中的值

回答by Frederik.L

You could make predefined tests to ensure that your function gives the correct output in different situations:

您可以进行预定义测试以确保您的函数在不同情况下提供正确的输出:

function test_getFileExtension() {
    test("foo.png", "png");
    test("bar.py", "py");
    test("the_really_long_name.txt", "txt");
    test("playlist.xspf", "xspf");
    test("%20.txt", "txt");
    test("file_without_extension", ""); // actually gives an error

    function test(input,output) {
        var o=getFileExtension(input);
        if (o === output) {
            console.log("OK: "+input+" --> "+o);
        } else {
            console.log("ERROR: "+input+" --> "+o);
        }
    }
}

Then as soon as you update your function, you call test_getFileExtension()again and make sure that it still work as intended.

然后一旦你更新你的函数,你就test_getFileExtension()再次调用并确保它仍然按预期工作。

回答by Pratap Sharma

There are several ways to print out line in Javascript. I'm listing few methods which works really well.

有几种方法可以在 Javascript 中打印出一行。我列出了一些非常有效的方法。

let a = 23;

console.log(a) // 23
process.stdout.write(a) //23
alert(a) //23

each of them would give the same result. Happy coding Cheers.

他们每个人都会给出相同的结果。快乐编码干杯。