0

我想让像 'å ä ö' 这样的字母可见。我想我需要用 ascii 代码替换这些字母。

我已经尝试过 jquery 和 javascript,但它不起作用。请看下面的代码:

    <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>
4

1 回答 1

2

您可以使用以下三种方法之一来实现您想要的。

密码笔

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

// 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;');

更好的解决方案

前两个代码示例的更好替代方法是将文件转换为正确的编码。为此,请确保您head的 HTML 文档中有此代码段。

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

如果这不起作用,您还必须确保使用编码 UTF-8 保存文件。如果您使用的是 Notepad++,这是通过Encoding > Encode in UTF-8.

于 2016-08-16T13:19:07.127 回答