【问题标题】:JQuery UI encoding nightmareJQuery UI 编码噩梦
【发布时间】:2012-12-05 08:13:30
【问题描述】:

我希望能够将任何字符串传递给 JQuery UI 中的按钮文本。

假设我有这个字符串:

Ajouter L'amie a la liste "amies"

在不导致大量 JavaScript 错误的情况下实际传递此文本的唯一方法是对其进行 HTML 编码。

registerhtml.dialog({
        modal: true,
        title: 'My Dialog Title',
        resizable: false,
        buttons: [
            {
                text: 'Ajouter L'amie a la liste "amies"',
                click: function(){
                    // Do something important
                    $(this).dialog('close');
                }
            },
            {
                text: 'Cancel',
                click: function() {
                    $(this).dialog('close');
                }
            }
        ]
    });

但按钮中没有任何人类可读的文本。只有相同的编码文本。

有没有快速解决这个问题的方法?也许我在按钮对象中缺少一个选项。

或者我应该戴上手套,打电话给护士,然后在 JQuery-ui.js 文件中开始手术?

【问题讨论】:

    标签: jquery jquery-ui html-encode html-escape-characters


    【解决方案1】:

    为了能够使用 htmlentities,您必须在将字符串插入按钮时使用 html 方法,因为 text 将直接插入文本而不进行实体编码:

    registerhtml.dialog({
        modal: true,
        title: 'My Dialog Title',
        resizable: false,
        buttons: [
            {
                html: 'Ajouter L'amie a la liste "amies"',
                click: function(){
                    // Do something important
                    $(this).dialog('close');
                }
            },
            {
                html: 'Cancel',
                click: function() {
                    $(this).dialog('close');
                }
            }
        ]
    });
    

    FIDDLE

    【讨论】:

      【解决方案2】:

      根据您是使用 ' 还是 " 作为字符串,使用 \

      转义字符串中 ' ör " 的所有出现

      例子:

      <!doctype html>
      <html>
          <head>
              <meta charset="utf-8">
              <title>Demo</title>
              <script src="jquery-1.8.2.min.js"></script>
              <script src="jquery-ui-1.9.0.custom.min.js"></script>
              <link href="jQueryUI_css/ui-lightness/jquery-ui-1.8.24.custom.css" rel="stylesheet" />
              <script>
                  $(function(){
      
                      //using ' to define a string
                      var text = 'Ajouter L\'amie a la liste "amies"'
                      $('div').text(text).button();
      
                      //using " to define a string
                      var text2 = "Ajouter L'amie a la liste \"amies\"";
                      $('button').text(text).button();
                  });
              </script>
          </head>
          <body>
          <div></div>
          <button></button>
          </body>
      </html>
      

      【讨论】:

      • 谢谢,但实际上,文本必须从服务器“按原样”发送......没有转义字符。在渲染时,ASP MVC 将决定何时需要编码。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      • 2020-03-14
      • 2010-11-13
      • 2010-12-22
      相关资源
      最近更新 更多