鱼C论坛

 找回密码
 立即注册
查看: 2400|回复: 6

[已解决]javascript format报错

[复制链接]
发表于 2020-8-27 21:52:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
为什么我在javascript中使用.format方法总是报错说"format is not a function",
我记得javascript中字符串有format方法的呀
最佳答案
2020-8-28 10:04:31
aaron.yang 发表于 2020-8-28 09:13
我在输入框中输入"/commands"时,就出错了

Javascript中的string是没有format方法的,建议使用字符串拼接,就像这样:
  1. answer.innerHTML = "<ul><li>" + commands[i] +"</li></ul>";
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-8-28 08:00:16 | 显示全部楼层
JS 中 format() 常用来处理 Date 对象,但对字符串进行格式化也不少

对象.format(xxx)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-28 08:08:57 | 显示全部楼层
不二如是 发表于 2020-8-28 08:00
JS 中 format() 常用来处理 Date 对象,但对字符串进行格式化也不少

对象.format(xxx)

那也应该有这个函数鸭,可报错内容是“.format is not a function”
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-28 08:53:53 | 显示全部楼层
aaron.yang 发表于 2020-8-28 08:08
那也应该有这个函数鸭,可报错内容是“.format is not a function”

发一下完整代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-28 09:11:57 | 显示全部楼层
本帖最后由 aaron.yang 于 2020-8-28 09:13 编辑
  1. <!DOCTYPE html>

  2. <html>
  3.     <head>
  4.         <style>
  5.             h1
  6.             {
  7.                 text-align: center;
  8.                 color: blue;
  9.             }
  10.             p
  11.             {
  12.                 font-size: large;
  13.             }
  14.             input
  15.             {
  16.                 height: 35px;
  17.                 width: 250px;
  18.                 font-size: 35px;
  19.             }
  20.             button
  21.             {
  22.                 height: 30px;
  23.                 width: 80px;
  24.             }
  25.         </style>
  26.         <meta charset="UTF-8">
  27.         <title>工具大师(web版)</title>
  28.     </head>
  29.     <body>
  30.         <h1>工具大师</h1>
  31.         <script type="text/javascript">
  32.             function main()
  33.             {
  34.                 var cmd = document.getElementById("greeting").value;
  35.                 var answer = document.getElementById("answer");
  36.                 var commands = ["/help", "/commands", "/date"];
  37.                 //console.log(cmd)
  38.                 if(cmd=="/help")
  39.                 {
  40.                     answer.innerHTML = "此功能暂未开启~";
  41.                 }
  42.                 else if(cmd=="/commands")
  43.                 {
  44.                     for(var i=0;i<commands.length;i++)
  45.                     {
  46.                         answer.innerHTML = "<ul><li>{0}</li></ul>".format(commands[i]);
  47.                     }
  48.                     // answer.innerHTML = "<ul><li>/help</li><li>/commands</li><li>/date</li></ul>"
  49.                 }
  50.                 else if(cmd=="/date")
  51.                 {
  52.                     answer.innerHTML = Date();
  53.                 }
  54.                 else
  55.                 {
  56.                     if(cmd[0]=='/')
  57.                     {
  58.                         answer.innerHTML = "没有此功能~";
  59.                     }
  60.                     else
  61.                     {
  62.                         answer.innerHTML = "指令要以"/"开头~";
  63.                     }
  64.                 }
  65.             }
  66.         </script>
  67.         <p>请输入指令: </p><input id="greeting" type="text">
  68.         <br><br>
  69.         <button type="submit" onclick="main()">提交</button>
  70.         <br><br>
  71.         <main id="answer">
  72.             <em>[no answer]</em>
  73.         </main>
  74.     </body>
  75. </html>
复制代码

我在输入框中输入"/commands"时,就出错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-28 09:13:30 | 显示全部楼层
  1. <!DOCTYPE html>

  2. <html>
  3.     <head>
  4.         <style>
  5.             h1
  6.             {
  7.                 text-align: center;
  8.                 color: blue;
  9.             }
  10.             p
  11.             {
  12.                 font-size: large;
  13.             }
  14.             input
  15.             {
  16.                 height: 35px;
  17.                 width: 250px;
  18.                 font-size: 35px;
  19.             }
  20.             button
  21.             {
  22.                 height: 30px;
  23.                 width: 80px;
  24.             }
  25.         </style>
  26.         <meta charset="UTF-8">
  27.         <title>工具大师(web版)</title>
  28.     </head>
  29.     <body>
  30.         <h1>工具大师</h1>
  31.         <script type="text/javascript">
  32.             function main()
  33.             {
  34.                 var cmd = document.getElementById("greeting").value;
  35.                 var answer = document.getElementById("answer");
  36.                 var commands = ["/help", "/commands", "/date"];
  37.                 //console.log(cmd)
  38.                 if(cmd=="/help")
  39.                 {
  40.                     answer.innerHTML = "此功能暂未开启~";
  41.                 }
  42.                 else if(cmd=="/commands")
  43.                 {
  44.                     for(var i=0;i<commands.length;i++)
  45.                     {
  46.                         answer.innerHTML = "<ul><li>{0}</li></ul>".format(commands[i]);
  47.                     }
  48.                     // answer.innerHTML = "<ul><li>/help</li><li>/commands</li><li>/date</li></ul>"
  49.                 }
  50.                 else if(cmd=="/date")
  51.                 {
  52.                     answer.innerHTML = Date();
  53.                 }
  54.                 else
  55.                 {
  56.                     if(cmd[0]=='/')
  57.                     {
  58.                         answer.innerHTML = "没有此功能~";
  59.                     }
  60.                     else
  61.                     {
  62.                         answer.innerHTML = "指令要以"/"开头~";
  63.                     }
  64.                 }
  65.             }
  66.         </script>
  67.         <p>请输入指令: </p><input id="greeting" type="text">
  68.         <br><br>
  69.         <button type="submit" onclick="main()">提交</button>
  70.         <br><br>
  71.         <main id="answer">
  72.             <em>[no answer]</em>
  73.         </main>
  74.     </body>
  75. </html>
复制代码

我在输入框中输入"/commands"时,就出错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-28 10:04:31 | 显示全部楼层    本楼为最佳答案   
aaron.yang 发表于 2020-8-28 09:13
我在输入框中输入"/commands"时,就出错了

Javascript中的string是没有format方法的,建议使用字符串拼接,就像这样:
  1. answer.innerHTML = "<ul><li>" + commands[i] +"</li></ul>";
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-13 05:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表