【问题标题】:JavaScript problem with special characters特殊字符的 JavaScript 问题
【发布时间】:2022-01-06 09:26:11
【问题描述】:

我开发了一个简单的表单,当我单击按钮时,它会检查所有字段是否输入正确。如果没有,字段下方会出现一条消息,非常标准

问题在于两个词,“最小”和“最大”。 位置正确,文字显示,但在线托管不正确。

页面上是<meta charset="UTF-8">,我也试过<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"/>。并且还插入了脚本标签<script src="js/contatPage.js" charset="UTF-8" type="module"></script><script src="js/contatPage.js" charset="iso-8859-1" type="module"></script>。这些都不起作用

接收输入、检查并调用函数以创建消息的函数

...
function check(e)
   ...
   {else if(e.value.length < e.minLength){
      createMessage(e, `Quantidade mínima de caracteres ${e.minLength}`) 
   }
   ...
...

错误示例图片

【问题讨论】:

  • 您将需要使用 HTML 字符。例如,mínima 应该使用 &amp;iacute;,因此 HTML 将应用正确的字符。检查这个:About Unicode。请记住,如果您使用 innerText 来设置消息,则需要使用 innerHTML 属性。
  • 你能展示将它添加到 DOM 的代码吗?
  • 您在 html 中指定了什么字符集?在head 标签中尝试&lt;meta charset="utf-8"&gt;

标签: javascript unicode character-encoding charset


【解决方案1】:

有些特殊字母不能直接输入到 HTML,必须用 HTML 特殊字符插入,主要都是在 W3Schools,而且大部分是在 VS Code 中添加的,所以当你在 HTML 中输入 '&'文件,它为你推荐了一些。查看更多关于here,在左侧导航。

在您的脚本中:

function check(e) // e is probably a text element, right?
{
   // Your conditions for message...
   createMessage(e, `Quantidade m&iacute;nima de caracteres ${e.minLength}`)
   // In this "createMessage" function, you will need to use the element's
   // innerHTML property, so the HTML character (&iacute = í) will be parsed
}

// For example...
function createMessage(e)
{
   e.target.innerHTML = "Sua mensagem &eacute; digitada aqui, meu jovem!";
}

祝你好运,希望这会有所帮助!

【讨论】:

  • 这很有意义,非常感谢。没有停下来想一想,vlw :)
猜你喜欢
  • 2011-04-19
  • 1970-01-01
  • 2012-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多