简体   繁体   English

字母替换

[英]Letter replacement

I want to make letters like 'å ä ö' visible. 我想让像“åäö”这样的字母可见。 I need to replace these letters with ascii code, I guess. 我想我需要将这些字母替换为ASCII码。

I have tried jquery and javascript, but it did not work. 我已经尝试过使用jquery和javascript,但是没有用。 Look at the following code please: 请看下面的代码:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    <script>


    jQuery("#body").html(
    jQuery("#body").html().replace('ä', '&aring;')
);


     document.body.innerHTML = document.body.innerHTML.replace(/ä/g, '&aring;');



    </script>



  </head>

  <body id="body">

    <div class="blog-masthead">
      <div class="container">
        <nav class="blog-nav">
          <a class="blog-nav-item active" href="index.php">Inlägg</a>
        </nav>
      </div>
    </div>

You can achieve what you want using one of the three methods below. 您可以使用以下三种方法之一实现所需的功能。

codepen 码笔

JQuery jQuery查询

// using a regex on the first parameter of replace, 
// picks all the 'ä' instead of the first one 
var replaced = $("body").html().replace(/ä/g,'&aring;'); 
$("body").html(replaced);

JavaScript 的JavaScript

// using a regex on the first parameter of replace, 
// picks all the 'ä' instead of the first one 
document.body.innerHTML = document.body.innerHTML.replace(/ä/g, '&aring;');

Better Solution 更好的解决方案

A better alternative to the two previous code sample is to convert your file to the right encoding. 前面两个代码示例的更好替代方法是将文件转换为正确的编码。 In order to do that, make sure you this snippet in the head of you HTML document. 为了做到这一点,请确保您在HTML文档的head具有此代码段。

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

If that's not working, you also have to make sure the file is saved with encoding UTF-8. 如果这不起作用,则还必须确保使用UTF-8编码保存文件。 If you're using Notepad++, this is done via Encoding > Encode in UTF-8 . 如果您使用的是Notepad ++,则可通过Encoding > Encode in UTF-8完成。

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

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