如何在HTML5数据属性中转义

时间:2012-08-15 19:18:26

标签: html json html5 custom-data-attribute

数据属性对我来说是新的,只是意识到我需要逃避空间,逗号等等。任何人都可以给我一些关于我的方法的建议吗?以下是我的代码:

#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)

1 个答案:

答案 0 :(得分:5)

将属性值放在双引号内,这样您就不必担心转义空格了。然后逃脱它就像你将转义引号中的任何其他属性。您的Web框架应具有此功能。