微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将 HTML 属性值拆分为多行

如何解决将 HTML 属性值拆分为多行

有没有办法格式化 HTML 属性值,使其跨越多行? 本质上,我问的是有没有办法在常规类型的 HTML 属性值中转义返回值。

例如,在下面的这个工作代码中,我们在一行中有 href 属性值。

<head>
  <!-- Bootstrap CSS -->
  <link
    rel="stylesheet" 
    href= "https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
  > <!-- href value above spans 1 line and wraps normally with text editors width -->
</head>
<body>
  <h1>Bootstrap has successfully applied because this font is sans-serif</h1>
</body>

我怎样才能使这种格式的不起作用代码具有跨越多行下面的属性值工作?:

<head>
  <!-- Bootstrap CSS -->
  <link
    rel="stylesheet" 
    href= "https://cdn.jsdelivr.net/npm/
           bootstrap@5.0.2/dist/css/
           bootstrap.min.css"
  > <!-- href value above spans 3 lines -->
</head>
<body>
  <h1>
    Bootstrap hasn't applied due to the amount of 
    <mark>enter</mark> characters used in the <mark>href</mark> attribute
  </h1>
</body>

似乎一旦您将返回值添加到值中,整个值就会失败。我尝试过常规的 javaScript 转义字符,但没有运气。

如何在 HTML 属性中成功添加返回 ( new lines )价值?

解决方法

我不相信 HTML 属性具有“属性”继续构造,例如 Javascript 或命令行中的行继续。

所以这真的行不通:

// This is just whitespace,so I assume the browser will encode it
href="https://cdn.jsdelivr.net/npm/
      bootstrap@5.0.2/dist/css/
      bootstrap.min.css"

// Javascript variety
href="https://cdn.jsdelivr.net/npm/" +
     "bootstrap@5.0.2/dist/css/" +
     "bootstrap.min.css"

// Command line variety
href="https://cdn.jsdelivr.net/npm/" \
     "bootstrap@5.0.2/dist/css/" \
     "bootstrap.min.css"

它只是属性中的空格,或者像上面示例中那样不受限制的引用字符串。

这是我能想到的最好的:

<script>
let link = document.createElement('link')

link.rel = 'stylesheet'
link.href = 'https://cdn.jsdelivr.net/' +
            'npm/bootstrap@5.0.2/' +
            'dist/css/bootstrap.min.css'

document.head.append(link)
</script>

可能能够做一些整理来查找和删除某些标签属性中不必要的空白。但是,如果重点是为了可读性而格式化浏览器的“查看源代码”标记,那么这将违背该目的。

,

能否请您分解一下您的问题!我很难理解,但如果你问的是格式化代码,我会使用代码编辑器,看看它们是否是格式化代码的功能。在 vs 代码中,它们是用于格式化代码的功能。但我不确定你有没有!对不起,如果你没有得到你所说的,但很高兴提出问题。继续努力吧!!! ?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。