JavaScript不适用于瑞典语字母

时间:2015-12-10 10:13:11

标签: javascript encoding

我的过滤javascript代码不适用瑞典字母åäö

<input name='tablefilter' type='checkbox' value='Linköping' id='tablefilter1' checked/>
<label for='tablefilter1'>Linköping</label>
<input name='tablefilter' type='checkbox' value='Mjölby' id='tablefilter2' checked/>
<label for='tablefilter2'>Mjölby</label>
<input name='tablefilter' type='checkbox' value='Norrköping' id='tablefilter3' checked/>
<label for='tablefilter3'>Norrköping</label>
<table>
  <thead>
    <tr>
      <th>Col1</th>
      <th>Col2</th>
      <th>Col3</th>
    </tr>
  </thead>
  <tbody id='tablebody'>
    <tr>
      <td>Linköping</td>
      <td>One</td>
      <td>First</td>
    </tr>
    <tr>
      <td>Mjölby</td>
      <td>Two</td>
      <td>Second</td>
    </tr>
    <tr>
      <td>Norrköping</td>
      <td>Three</td>
      <td>Third</td>
    </tr>
  </tbody>
</table>

JS

/* Demo filtering table using checkboxes. Filters against first td value */

/* Set 'ready' handler' */
document.addEventListener('DOMContentLoaded', initFunc);

/* When document ready, set click handlers for the filter boxes */
function initFunc(event) {
  var filters = document.getElementsByName('tablefilter');
  for (var i = 0; i < filters.length; i++) {
    filters[i].addEventListener('click', buildAndExecFilter);
  }
}

/*
    This function gets called when clicking on table filter checkboxes.
    It builds a list of selected values and then filters the table based on that
*/
function buildAndExecFilter() {
  var show = [];
  var filters = document.getElementsByName('tablefilter');
  for (var i = 0; i < filters.length; i++) {
    if (filters[i].checked) {
      show.push(filters[i].value);
    }
  }
  execFilter(show); // Filter based on selected values
}

function execFilter(show) {
  /* For all rows of table, see if td 0 contains a selected value to filter */
  var rows = document.getElementById('tablebody').getElementsByTagName('tr');
  for (var i = 0; i < rows.length; i++) {
    var display = ""; // Default to display
    // If it is not found in the selected filter values, don't show it
    if (show.indexOf(rows[i].children[0].textContent) === -1) {
      display = "none";
    }
    // Update the display accordingly
    rows[i].style.display = display;
  }
}

http://jsfiddle.net/2Lm7pytt/4/

它适用于jsfiddle,但它不适用于我的visual studio。问题是它无法显示瑞典语字母,因为它可以。

问题是JavaScript没有使用瑞典语字母。

我该怎么做才能让它发挥作用?

2 个答案:

答案 0 :(得分:1)

您可以尝试添加到head标记的顶部

  

&LT; meta charset =“utf-8”/&gt;

答案 1 :(得分:0)

这可能会在几个点失败,但由于编码问题很可能在比较中失败,因此我建议使用调试器在此行之前断开JavaScript:

if (show.indexOf(rows[i].children[0].textContent) === -1) {
     display = "none";
}

检查textContent中包含的内容,尝试在立即窗口中进行比较,例如rows[i].children[0].textContent === "Linköping"。如果你发现这个错误,这可能会证实。

转到FILE&gt; Visual Studio中的高级保存选项并检查此处设置的编码选项 - 我使用的是“Unicode(带签名的UTF-8) - 代码页65001”,我的代码包含下面答案中Brandon建议中的元代码。有了这些设置,它就按照预期运行了。