简体   繁体   English

函数不会返回q -Javascript

[英]function won't return q -Javascript

I have the following function: 我有以下功能:

  "use strict";
    $(document).ready(function(){
        $("#slider,input[type='range']").on("mousemove touchmove ",function(){
            $("#test").html($(this).val())
        });
    });

        function AddClass(){
            var q=0;
            var p=prompt("Enter the class(es) you want to add seperated by a ','");
            var to_be_added=[];
            to_be_added=p.split(",");
            for (var j=0;j<to_be_added.length;j++){
                if(to_be_added[j][0] === "."){
                    to_be_added[j]=to_be_added[j].slice(1);//check if the first character of each class inside an array contains "." and remove it
                }
            }
            for (var k=0;k<to_be_added.length;k++){
                $("#slider").addClass(to_be_added[k]);
                q++;
            }
            return "q";
        }
        function delclass(){
            var q=0;
            var p=prompt("Enter the class(es) you want to remove seperated by a ','");
            var to_be_added=[];
            to_be_added=p.split(",");
            for (var j=0;j<to_be_added.length;j++){
                if(to_be_added[j][0] === "."){
                    to_be_added[j]=to_be_added[j].slice(1);//check if the first character of each class inside an array contains "." and remove it
                }
            }
            for (var k=0;k<to_be_added.length;k++){
                $("#slider").removeClass(to_be_added[k]);
                q++;
            }
            return "Hello";

        }

html: HTML:

<button onclick="delclass()">Delete</button>

The problem is that after running the above code i don't get anything in the console.Not even undefined.Any ideas?Jquery is included and $("#slider") is correct. 问题是运行上述代码后,控制台中什么也没得到。甚至没有未定义。有任何想法吗?包含了jQuery,并且$(“#slider”)是正确的。 Update If call the function from the console i will get "Hello" in the console but when i use button to call that function i get nothing 更新如果从控制台调用该函数,我将在控制台中看到“ Hello”,但是当我使用按钮调用该函数时,我什么也没得到

when i use button to call that function i get nothing 当我使用按钮调用该功能时,我什么也没得到

This is to be expected. 这是预料之中的。 The console does not show the return value of every function that executes in your code. 控制台不会显示在代码中执行的每个函数的返回值。 If you manually execute code in the console, the console will show you the result of that execution. 如果您在控制台中手动执行代码,则控制台将向您显示该执行的结果。 That is different from having code execute in your page. 这与在页面中执行代码不同。

Returning a value from an event handler does nothing, and it certainly does not output anything to the console. 从事件处理程序返回值不会执行任何操作,并且它肯定不会向控制台输出任何内容。

If you want to display a value in the console, use console.log : 如果要在控制台中显示值,请使用console.log

console.log("Hello");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM