【问题标题】:jQuery Mobile quotes inside quotes inside quotesjQuery Mobile 引号内引号内引号
【发布时间】:2014-01-22 15:19:30
【问题描述】:

我在 jQuery Mobile 中有一种情况,我正在生成一个超链接,其中引号内的引号内有引号。我已经在其他帖子中搜索了可能的解决方案,但似乎没有任何效果。这是原始代码:

$("#char_list").append("<div data-role='collapsible'>
<h4> " + charName + "</h4><p><strong>Number of Actions:</strong> " + charNumActions + 
"</p><a data-role='button' class='edit_button'    
onClick='sessionStorage.setItem('editCharName', '" + charName + "')' 
data-icon='edit' data-iconpos='left'>Edit</a><a data-role='button' id='delete_char_button' 
class='delete_button' data-inline='true' data-icon='delete' data-iconpos='left'>Delete</a></div>");

问题出在 onclick 部分;它会产生语法错误,我理解原因。我在单引号之后使用了单引号。

所以,在搜索了这些论坛之后,我尝试像这样转义第二组引号(为简洁起见,仅放入 onclick 部分):

 onClick='sessionStorage.setItem(\"editCharName\", '\"" + charName + "\")'

这不会产生语法错误,但会使按钮无用;我点击它,什么都没有发生。 然后我也从这个论坛尝试了这个:

 onClick='sessionStorage.setItem(&quot;editCharName&quot;, '&quot;" + charName + "&quot;)'

这与转义引号具有相同的效果。也就是说,当我单击按钮时,什么也没有发生。

必须有办法做到这一点。如果有人有任何建议,请告诉我。谢谢!

【问题讨论】:

    标签: javascript jquery quotes double-quotes


    【解决方案1】:

    像这样?

    $("#char_list").append("<div data-role='collapsible'>" +
        "<h4>" + charName + "</h4>" +
        "<p><strong>Number of Actions:</strong>" + charNumActions + "</p>" +
        "<a data-role='button' class='edit_button' data-icon='edit' data-iconpos='left'>Edit</a>" +
        "<a data-role='button' id='delete_char_button' class='delete_button' data-inline='true' data-icon='delete' data-iconpos='left'>Delete</a>" +
        "</div>");
    
    $(document).on('click','.edit_button',function(){
        var charName = $(this).parents('[data-role="collapsible"]').find('h4').html();
        sessionStorage.setItem('editCharName',charName);
    });
    
    • 删除内联 JS / 以 jQuery 样式制作
    • 让它更具可读性!

    【讨论】:

    • 哇。这就是我试图在 3 小时的睡眠中找出问题所得到的结果。如此简单的解决一个简单的问题。太感谢了。 :-) @l2aelba
    猜你喜欢
    • 1970-01-01
    • 2013-09-24
    • 2013-11-19
    • 1970-01-01
    • 2014-02-02
    • 2015-01-14
    • 2011-09-10
    • 1970-01-01
    • 2016-12-17
    相关资源
    最近更新 更多