简体   繁体   English

HTML中的Javascript调用函数

[英]Javascript call function inside html

My database is giving me week days as "0", "1", "3", "4", "5", "6". 我的数据库为我提供了“ 0”,“ 1”,“ 3”,“ 4”,“ 5”,“ 6”的工作日。 And I wanted to translate it to their actual name. 我想将其翻译成他们的真实姓名。

So I'm doing: 所以我在做:

HMTL HMTL

<div><script>traduzir("<?php echo $row_room['weekday']?>")</script></div> 

JS JS

<script>
    function traduzir (woord){
        if (woord == "0")
        {
            return "Domingo";
        }

        if (word == "1")
        {
            return "Segunda";
        }

        if (word == "2")
        {
            return "Terça";
        }

        if (word == "3")
        {
            return "Quarta";
        }

        if (word == "4")
        {
            return "Quinta";
        }

        if (word == "5")
        {
            return "Sexta";
        }

        if (word == "6")
        {
            return "Sabado";
        }
    };

</script>

I'm getting "traduzir is not defined" error. 我收到“未定义Traduzir”错误。 Can someone help? 有人可以帮忙吗? Thanks! 谢谢!

First you need to make sure the script that defines the traduzir method is placed before the script that calls it. 首先,您需要确保定义traduzir方法的脚本位于调用它的脚本之前。

Then you need to also change your script so that it actually does something with the returned value. 然后,您还需要更改脚本,以便它实际上使用返回的值执行某些操作。

If the section marked JS is in a separate JavaScript file, then remove the HTML ( <script> and </script> ). 如果标记为JS的部分在单独的JavaScript文件中,则删除HTML( <script></script> )。

In either case, move the second script so the HTML document loads it before the first script. 无论哪种情况,都移动第二个脚本,以便HTML文档在第一个脚本之前加载它。 You can't use a function until you have defined it, and it won't be hoisted from a later script in time to use it immediately in an earlier one. 您必须先定义一个函数,然后才能使用它,也不能及时从后来的脚本中吊起它,以便在更早的版本中立即使用它。

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

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