如何为HTML标签属性使用多行值? (即如何逃脱换行符?) [英] How can I use a multiline value for an HTML tag attribute? (i.e. how do I escape newline?)

查看:133
本文介绍了如何为HTML标签属性使用多行值? (即如何逃脱换行符?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在HTML标签属性中包含换行符?

How do I include a newline in an HTML tag attribute?

例如:

<a href="somepage.html" onclick="javascript: foo('This is a multiline string.
This is the part after the newline.')">some link</a>


编辑:很抱歉,不好的例子,如果代码恰巧不在javascript中,该怎么办,说:


Edit: Sorry, bad example, what if the tag happened to not be in javascript, say:

<sometag someattr="This is a multiline string.
This is the part after the newline." />


编辑2 :原来字符串中的换行符不是我的问题,这是我正在调用的javascript函数. FWIW,"&#10;"可用于HTML属性中的换行符.


Edit 2: Turns out the newline in the string wasn't my problem, it was the javascript function I was calling. FWIW, "&#10;" can be used for newline in an HTML attribute.

推荐答案

根据我对HTML标准的记忆,字符实体在属性中起作用,因此可能可以起作用:

From what I remember about the HTML standard, character entities work in attributes, so this might work:

<sometag someattr="This is a multiline string.&#10;This is the part after the newline." />

我不确定您想要的换行符"是否应为&#10; (\ n)或&#13; (\ r \ n),我不确定浏览器是否会按照您想要的方式解释它.

I'm not sure if the "newline" you want ought to be &#10; (\n) or &#13;&#10; (\r\n), and I'm not sure if browsers will interpret it the way you want.

您为什么需要它?您想通过在HTML标签属性中添加换行符来解决什么具体问题?

Why do you need it? What specific problem are you trying to solve by adding a newline in an HTML tag attribute?

这篇关于如何为HTML标签属性使用多行值? (即如何逃脱换行符?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆