1

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

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

1 回答 1

5

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

于 2012-08-15T19:21:03.113 回答