如何防止树枝自动转义日文字符?

我正在尝试显示产品的到货日期。我正在使用moment.js将YYYY/MM/DD日期字符串格式化为日语本地字符串。我的 javascript 代码包含在一个twig文件中:


<script>

        moment.locale('ja');

        let updateArrivalDate = function() {

            $('.arrival-date').each(function() {

                let $this = $(this);

                $selectDate = $this.next().next().find('select').eq(0);

                $selectTime = $this.next().next().find('select').eq(1);

                var text = $selectDate.val();

                var date = moment(text, 'YYYY/MM/DD');

                if (date.isValid()) {

                    $this.text(date.format('{{ 'YYYY年M月D日 (dd)'|raw }}'))

                }

            });

        };

        $(document).ready(function() {

          updateArrivalDate();

            $('select').on('change', function() {

                updateArrivalDate();

            });

        });

    </script>

如您所见,我使用raw过滤器来防止树枝转义日文字符。尽管如此,twig 还是会转义特殊字符,并且文本会出现乱码:

http://img.mukewang.com/618e18d50001a9b905750188.jpg

当然,如果我将上面的代码段移到外部文件中,它就会解决。但说真的,有没有办法阻止树枝转义日文字符?为什么raw过滤器工作?


拉风的咖菲猫
浏览 147回答 2
2回答

互换的青春

我认为您可以像这样更改代码:$this.text(date.format('{{&nbsp;"YYYY年M月D日&nbsp;(dd)"|json_encode()|raw&nbsp;}}'))

开满天机

ja如https://momentjs.com/docs/#/i18n/ 中所示,从 cdn(或从 npm/yarm 安装包)导入 moment语言环境如果您使用 cdn 托管的 js,则使用以下代码:<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js" integrity="sha256-4iQZ6BVL4qNKlQ27TExEhBN1HFPvAvAMbFavKKosSWQ=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/locale/ja.js" integrity="sha256-CFWtR1hGN/5Vc+kcJkqeMFND0g6gFFZdnSqUtdL7WOQ=" crossorigin="anonymous"></script>然后而不是:&nbsp;$this.text(date.format('{{ 'YYYY年M月D日 (dd)'|raw }}'))用:&nbsp;var date = moment(text, 'YYYY/MM/DD');&nbsp;$this.html(date.locale('ja').format('LL (dd)'))并删除:&nbsp; &nbsp; &nbsp; &nbsp; moment.locale('ja');线所以你的脚本将是:<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js" integrity="sha256-4iQZ6BVL4qNKlQ27TExEhBN1HFPvAvAMbFavKKosSWQ=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/locale/ja.js" integrity="sha256-CFWtR1hGN/5Vc+kcJkqeMFND0g6gFFZdnSqUtdL7WOQ=" crossorigin="anonymous"></script><script>&nbsp; &nbsp; &nbsp; &nbsp; let updateArrivalDate = function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('.arrival-date').each(function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let $this = $(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $selectDate = $this.next().next().find('select').eq(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $selectTime = $this.next().next().find('select').eq(1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let text = $selectDate.val();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let date = moment(text, 'YYYY/MM/DD');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (date.isValid()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this.text(date.locale('ja').format('LL (dd)'))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; $(document).ready(function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateArrivalDate();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('select').on('change', function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateArrivalDate();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; </script>请参阅下面的示例,其中仅包含有关语言的格式:$(document).ready(function(){&nbsp;$("#date").html(moment().locale('ja').format('LL (dd)'))});<script src="https://momentjs.com/downloads/moment.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/locale/ja.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div id="date"></div>所以你让时间格式化日期并避免使用 js/twig 的任何复杂性。保持简单,让 js 完成它的工作,而不是将它们混合在一起。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript