JQuery UI 编码噩梦



我希望能够将任何字符串传递给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文件中开始手术?

为了能够使用 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');
            }
        }
    ]
});

小提琴

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

例:

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

最新更新