【问题标题】:HTML: Get text made up from different elements be shown in one line (link and text)HTML:获取由不同元素组成的文本显示在一行中(链接和文本)
【发布时间】:2019-04-12 14:14:01
【问题描述】:

因此,在我的网页中,用户可以更改语言。这很容易实现,但是问题在于由多个元素组成的文本。

想象一下:“...有关更多信息,请单击此处。”以“这里”为链接。

起初,我只是用<p <a>/> 做的,但是,这导致文本不再在一行中,而是在不同的行中。

这是有问题的代码:

                    <div align="center" >                       
                        <font color="#534f4f" size="+2">    
                            <div id="txt_whatwedo_learnmore">
                                To learn more about what exactly we can do for you and further 
                                info about timelines and pricing, click 
                            </div>                      
                            <a id="txt_whatwedo_learnmore2" style="text-decoration:none" color="black" 
                            href="./pdf/Pricing_App_Dev_2019_Ger.pdf">here.</a> 
                            <div id="txt_whatwedo_learnmore3">
                                (prices may vary).
                            </div>                          
                        </font>
                    </div>

我可以通过脚本通过 ID 访问每个元素,但它们都位于不同的行中,如下所示:

我如何让它在一行中,每个元素仍然有其唯一的 ID?

【问题讨论】:

  • 字体标签已过时,不应使用,div 上的对齐属性无效(改用 css)。但是要让你的元素都出现在一行中,请将它们设置为内联或内联块

标签: javascript html


【解决方案1】:

使用&lt;span&gt; 标签而不是&lt;div&gt; 标签。跨度为display: inline,div 为display: block;

<div align="center">                       
    <font color="#534f4f" size="+2">    
        <span id="txt_whatwedo_learnmore">
            To learn more about what exactly we can do for you and further 
            info about timelines and pricing, click 
        </span>                      
        <a id="txt_whatwedo_learnmore2" style="text-decoration:none" color="black" 
        href="./pdf/Pricing_App_Dev_2019_Ger.pdf">here.</a> 
        <span id="txt_whatwedo_learnmore3">
            (prices may vary).
        </span>                          
    </font>
</div>

另外,请不要使用&lt;font&gt;align="center"color="black",这些都是90年代初期的非常古老的HTML,不应该再使用了!使用 CSS 可以更轻松地操作此代码,您也应避免使用内联 style="..."

这里有一个更好的想写相同的代码:

#learn-more {
  color: #534f4f;
  font-size: 24px;
  text-align: center;
}

#learn-more a {
  text-decoration: none;
  color: black;
}
<div id="learn-more">
  <span id="txt_whatwedo_learnmore">
    To learn more about what exactly we can do for you and further 
    info about timelines and pricing, click 
  </span>
  <a id="txt_whatwedo_learnmore2" href="./pdf/Pricing_App_Dev_2019_Ger.pdf">here.</a>
  <span id="txt_whatwedo_learnmore3">
    (prices may vary).
  </span>
</div>

【讨论】:

  • 不客气!如果对您有帮助,请将其标记为正确答案。
  • 是的。定义。它只是告诉我必须已经过了 10 分钟;)
  • + 感谢您的建议。我刚开始做html。我会记住这一点:)
【解决方案2】:

div 元素是块级元素,块级元素出现在屏幕上就好像它们前后有一个换行符。你可以使用display: inline

#txt_whatwedo_learnmore,
#txt_whatwedo_learnmore3{
  display: inline;
}
<div align="center" >                       
  <font color="#534f4f" size="+2">    
      <div id="txt_whatwedo_learnmore">
          To learn more about what exactly we further 
          info about timelines and pricing, click 
      </div>                      
      <a id="txt_whatwedo_learnmore2" style="text-decoration:none" color="black" 
      href="./pdf/Pricing_App_Dev_2019_Ger.pdf">here.</a> 
      <div id="txt_whatwedo_learnmore3">
          (prices may vary).
      </div>                          
  </font>
</div>

【讨论】:

    【解决方案3】:

    你不应该使用font 标签,它已经过时了。您的 div 的 align 属性已被弃用,所以最好不要也使用这个。

    对于将 div 放在一行中,请查看 display: inline-block

    div {
      font-weight: bold;
      /** Adjust font weight */
      color: grey;
      /** Adjust fonf color */
      display: inline-block; /** to have divs in one line */
    }
    <div id="container">
      <div id="txt_whatwedo_learnmore">
        To learn more about what exactly we can do for you and further info about timelines and pricing, click
      </div>
      <a id="txt_whatwedo_learnmore2" style="text-decoration:none" color="black" href="./pdf/Pricing_App_Dev_2019_Ger.pdf">here.</a>
      <div id="txt_whatwedo_learnmore3">
        (prices may vary).
      </div>
    </div>
    猜你喜欢
    • 1970-01-01
    • 2017-05-19
    • 2019-02-02
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    • 2021-05-13
    相关资源
    最近更新 更多