【问题标题】:Making multiple paragraphs show up as one使多个段落显示为一个
【发布时间】:2021-12-17 23:46:07
【问题描述】:

我正在尝试制作一个工具,可以根据您的鞋码计算您需要多大的滑板。代码如下:

const shoeSizeInput = document.getElementById('shoeSize')
      const shoeSizeResult = document.getElementById('resultSize') 

shoeSizeInput.addEventListener('input', (event) => {
  const shoeSize = parseInt(event.target.value) 
  let boardSize = '?'

  switch (true) {
    case 0 <= shoeSize && shoeSize <= 7:
      boardSize = '7.75'
      break;
    case shoeSize === 8 || shoeSize === 9:
      boardSize = '8'
      break;
    case shoeSize === 10 || shoeSize === 11:
      boardSize = '8.25'
      break;
    case shoeSize === 12 || shoeSize === 13:
      boardSize = '8.38'
      break;
    case 14 <= shoeSize && shoeSize <= 20:
      boardSize = '8.5'
      break;
  }
  shoeSizeResult.textContent = boardSize 
})
<div class="board-tool">
    <p>Most people pick their board size by prefrence but I will make a tool below to choose a board size that will fit your shoe size best. The most popular board sizes are 7.75, 8, 8.25, 8.38, and 8.5.</p>
    <label>If your shoe size is:</label><input id='shoeSize' type="number" class="shoe">
    <p id="resultSize"></p><p>should be your ideal board size.</p>
  </div>
.shoe {
  width: 50px;
  height: 15px;
  border-radius: 5px;
  margin-left: 10px;
}
.board-tool {
  font-size: 1.5rem;
}

所有代码都有效,但问题是第一段、标签和之后的段落都在不同的行上,但我想组织它,使其看起来像一个段落,例如: 如果你的鞋码是:12,8.25 将是理想的板码。

【问题讨论】:

    标签: html css inline paragraph


    【解决方案1】:

    有几种方法可以解决您的问题。但我认为首先是对 HTML 元素有一个基本的了解。

    &lt;p&gt;(段落)元素是块项目,这意味着默认情况下,它们的宽度是其包含元素的 100%,并且它们具有默认的顶部和底部边距,旨在提供元素之间的一些默认间距。

    &lt;span&gt; 元素是内联的。它们一个接一个地连续运行,这样它们就可以作为一个段落来阅读。

    但是,现在事情要先进得多,并且很有可能使用样式和类规则将 &lt;p&gt; 重新定义为 &lt;span&gt;,反之亦然,如下面的 sn-p 所示。

    虽然我不建议养成这种习惯,但可以做到。跟随您的代码的示例 div 使用浅灰色虚线边框,因此您可以看到块与内联元素的默认宽度

    const shoeSizeInput = document.getElementById('shoeSize')
    const shoeSizeResult = document.getElementById('resultSize')
    
    shoeSizeInput.addEventListener('input', (event) => {
      const shoeSize = parseInt(event.target.value)
      let boardSize = '?'
    
      switch (true) {
        case 0 <= shoeSize && shoeSize <= 7:
          boardSize = '7.75'
          break;
        case shoeSize === 8 || shoeSize === 9:
          boardSize = '8'
          break;
        case shoeSize === 10 || shoeSize === 11:
          boardSize = '8.25'
          break;
        case shoeSize === 12 || shoeSize === 13:
          boardSize = '8.38'
          break;
        case 14 <= shoeSize && shoeSize <= 20:
          boardSize = '8.5'
          break;
      }
      shoeSizeResult.textContent = boardSize
    })
    .shoe {
      width: 50px;
      height: 15px;
      border-radius: 5px;
      margin-left: 10px;
    }
    
    .board-tool {
      font-size: 1.5rem;
    }
    /* End OP CSS */
    
    /* Begin Example CSS */
    
    .d-inline {
      display: inline;
    }
    
    .d-block {
      display: block;
    }
    
    .m-none {
      margin: 0;
    }
    
    .mt {
      margin-top: 10px;
    }
    
    .mtb {
      margin: 10px 0;
    }
    
    .b {
      border: 1px solid green;
    }
    
    div.example>div {
      padding: 10px;
    }
    
    div.example>div>p,
    div.example>div>span {
      border: 1px dashed #ccc;
    }
    <div class="board-tool">
      <p>Most people pick their board size by prefrence but I will make a tool below to choose a board size that will fit your shoe size best. The most popular board sizes are 7.75, 8, 8.25, 8.38, and 8.5.</p>
      <p>If your shoe size is: <input id='shoeSize' type="number" class="shoe">
        <span id="resultSize"></span>
        <span>should be your ideal board size.</span>
      </p>
    </div>
    
    <div class="example">
      <div class="mt b">
        <p>paragraphs </p>
        <p>as default</p>
        <p>block items.</p>
      </div>
    
      <div class="mt b">
        <p class="d-inline m-none">paragraphs </p>
        <p class="d-inline m-none">styled as </p>
        <p class="d-inline m-none">spans.</p>
      </div>
    
      <div class="mt b">
        <span class="d-block mtb">span elements </span>
        <span class="d-block mtb">styled as </span>
        <span class="d-block mtb">paragraph elements.</span>
      </div>
    </div>

    【讨论】:

      【解决方案2】:

      第 1 步:

      .board-tool 更改为display: flex

      第 2 步:

      .board-too 更改为flex-wrap: wrapalign-items: center

      第 3 步: 给你的最后一段margin-left 10px

      代码:

      CSS:

      .shoe {
        width: 50px;
        height: 15px;
        border-radius: 5px;
        margin-left: 10px;
      }
      
      .board-tool {
        font-size: 1.5rem;
        display: flex;
        flex-wrap: wrap;
        align-items: center;
      }
      
      .resultLabel {
        margin-left: 10px;
      }
      

      HTML:

          <div class="board-tool">
          <p>Most people pick their board size by prefrence but I will make a tool below to choose a board size that will fit
              your shoe size best. The most popular board sizes are 7.75, 8, 8.25, 8.38, and 8.5.</p>
          <label>If your shoe size is:</label><input id='shoeSize' type="number" class="shoe">
          <p id="resultSize"></p>
          <p class="resultLabel">should be your ideal board size.</p>
      </div>
      

      javascript没有变化

      有关 flexbox 的更多信息,请访问W3Schools: Flexbox

      【讨论】:

        【解决方案3】:

        p 元素是“段落”,因此默认情况下会开始一个换行符。

        您可以将这些 p 元素更改为 span 元素。

        const shoeSizeInput = document.getElementById('shoeSize')
        const shoeSizeResult = document.getElementById('resultSize')
        
        shoeSizeInput.addEventListener('input', (event) => {
          const shoeSize = parseInt(event.target.value)
          let boardSize = '?'
        
          switch (true) {
            case 0 <= shoeSize && shoeSize <= 7:
              boardSize = '7.75'
              break;
            case shoeSize === 8 || shoeSize === 9:
              boardSize = '8'
              break;
            case shoeSize === 10 || shoeSize === 11:
              boardSize = '8.25'
              break;
            case shoeSize === 12 || shoeSize === 13:
              boardSize = '8.38'
              break;
            case 14 <= shoeSize && shoeSize <= 20:
              boardSize = '8.5'
              break;
          }
          shoeSizeResult.textContent = boardSize
        })
        .shoe {
          width: 50px;
          height: 15px;
          border-radius: 5px;
          margin-left: 10px;
        }
        
        .board-tool {
          font-size: 1.5rem;
        }
        <div class="board-tool">
          <p>Most people pick their board size by prefrence but I will make a tool below to choose a board size that will fit your shoe size best. The most popular board sizes are 7.75, 8, 8.25, 8.38, and 8.5.</p>
          <label>If your shoe size is:</label><input id='shoeSize' type="number" class="shoe">
          <span id="resultSize">?</span><span> should be your ideal board size.</span>
        </div>

        【讨论】:

          猜你喜欢
          • 2022-01-14
          • 1970-01-01
          • 2021-03-11
          • 1970-01-01
          • 2010-11-10
          • 2013-10-17
          • 2021-01-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多