Javascript使用.text时获取到错误的字符集,出现在使用日语字符时。

huangapple go评论59阅读模式
英文:

Javascript gets wrong charset when using .text with Japanese characters

问题

I have a javascript function that toggles the text of a button when clicked:

$(function() {
    $(".trigger-block").hide();
    $(".trigger").click(function() {
        $(this).parents(".more-cases").find(".trigger-block").slideToggle();
        $(this).toggleClass("close");
        $(this).text($(this).text() == "その他の事例を見る" ? "その他の事例を閉じる" : "その他の事例を見る");
    });
});

It works with English but it outputs squares with question marks if I use Japanese.
I have charset=UTF-8 inside </head> and also put it on the script tag with no success:

<script src="js/page.js" charset="UTF-8"></script>

Can someone guide me to the solution? I've searched on various Japanese forums and haven't found an answer. I've had success using Japanese with .append so I don't know what is causing this problem.

EDIT: .append works on a different js file but not this one. I'm still trying to figure out what is different between those files.

英文:

I have a javascript function that toggles the text of a button when clicked:

$(function() {
        $(&quot;.trigger-block&quot;).hide();
        $(&quot;.trigger&quot;).click(function() {
            $(this).parents(&quot;.more-cases&quot;).find(&quot;.trigger-block&quot;).slideToggle();
            $(this).toggleClass(&quot;close&quot;);
			$(this).text($(this).text() == &quot;その他の事例を見る&quot; ? &quot;その他の事例を閉じる&quot; : &quot;その他の事例を見る&quot;);
        });
    });

It works with English but it outputs squares with questions marks if I use Japanese.
I have charset=UTF-8 inside </head> and also put it on the script tag with no success.

&lt;script src=&quot;js/page.js&quot; charset=&quot;UTF-8&quot;&gt;&lt;/script&gt; 

Can some guide me to the solution? I've searched on various Japanese forums and haven't found an answer. I've had success using Japanese with .append so I don't know what is causing this problem.

EDIT: .append works on a different js file but not this one. I'm still trying to figure out what is different between those files.

答案1

得分: 1

原来我正在编辑的原始文件没有正确编码。我通过简单地创建一个新的.js文件,并将原始代码复制到其中来解决了这个问题。感谢大家的支持!

英文:

Turns out the original file I was editing was not encoded properly. I've reached a solution by simply making a new .js file, and copying the original code to it. Thank you all for the support!

huangapple
  • 本文由 发表于 2023年2月10日 10:53:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定