#253·htm

在元素附近裁剪换行

作者: kurumpa创建于 2023年11月14日更新于 2023年11月14日

当运行以下代码时:

javascript
import htm from 'https://unpkg.com/htm?module';

function h(type, props, ...children) {
  return { type, props, children };
}

const html = htm.bind(h);

console.log(html`<div>
        <b>bold</b>
        regular
    </div>`);

我得到以下对象:

javascript
{
  type: 'div',
  props: null,
  children: [{type: 'b' /* , ... */}, 'regular']
}

字符串已自动格式化,但仍然看起来是有效的 HTML 代码。现在,我已经实现了自己的 h() 函数,因为我想以自己的方式组合 DOM 元素。然而,我没有看到任何明显的方法来用空格将 boldregular 元素分隔开,除了插入 &nbsp; (#234)。对此,我非常欢迎任何评论。