【问题标题】:How to escape in HTML5 data-attribute如何在 HTML5 数据属性中转义
【发布时间】:2015-10-13 11:58:35
【问题描述】:

数据属性的东西对我来说是新的,只是意识到我需要转义空格、逗号等。有人可以就我的方法给我一些建议吗?以下是我的代码:

#A list holds all the strings to be sent to data-attribute, 
#the sequence and length of the list is dynamic, determined by the user 
#(That's why I need to find a way to escape automatically.

Apt_p = []
Apt_p.append('Relative to planting')
Apt_p.append('Relative to harvest')
Apt_p_j=json.dumps(Apt_p) #convert everything into JSON for future usage

#I think below is the place to let escape happen.
html = """<td id="Apt_p_j" data-val=%s></td>""" %(Apt_p_j)

【问题讨论】:

    标签: json html custom-data-attribute


    【解决方案1】:

    将您的属性值放在双引号内,这样您就不必担心转义空格。然后转义它,就像转义引号中的任何其他属性一样。你的网络框架应该有一个功能。

    【讨论】:

    • 我在 data-attribute(data-val="%s") 中添加了双引号,但现在返回的是 '['。但后来我添加了单引号(data-val='%s')。有用!谢谢!
    • 我不知道 fire bug 是否能让你看到 DOM 的实际来源或当前状态。并不是说双引号不起作用:通常它们可以正常工作,但是您显然忘记了在 json 字符串中转义双引号。
    猜你喜欢
    • 2011-11-07
    • 2013-02-15
    • 2016-08-19
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    相关资源
    最近更新 更多