【问题标题】:Force a line break in a URL在 URL 中强制换行
【发布时间】:2011-05-18 14:06:14
【问题描述】:

我的博客上有 Twitter 提要。它工作得很好,但是推文中的长 URL 存在问题。长 URL 会超出容器的宽度,从而破坏布局。

我的代码如下所示:

<ul id="twitter_update_list">
    <!-- twitter feed -->
</ul>

<script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/USERNAME.json?callback=twitterCallback2&amp;count=3"></script>

blogger.js 脚本包含回调函数,该函数从 Twitter 请求中获取数据并将&lt;li&gt; 元素填充到预定义的&lt;ul&gt;

我正在使用以下 CSS 来自动换行(对于支持它的浏览器):

#twitter_update_list li span a {
    word-wrap: break-word;
}

我知道&lt;wbr&gt; 标记,并试图将它与如下所示的 jquery 函数一起使用:

$(document).ready(function(){
    $("#twitter_update_list li span a").each(function(){
        // a replaceAll string prototype was used here to replace "/" with "<wbr>/"
    });
});

但是,当我尝试使用该 sn-p 时,它会导致 IE 停止响应,这不好。

我正在寻找可以解决换行问题的代码块(通过向长 URL 添加换行符)。 Firefox 和 Chrome 运行正常,但 IE7 和 IE8 需要更多。 (我不关心IE6。)

【问题讨论】:

    标签: html css


    【解决方案1】:

    您可以尝试使用:word-break: break-all;

    div {
      background: lightblue;
      border: 1px solid #000;
      width: 200px;
      margin: 1em;
      padding: 1em;
    }
    .break {
      word-break: break-all;
    }
    <div>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Cupiditate vitae fugiat fuga fugit cum <a href="" class="break">http://www.ThisLongTextBreaksBecauseItHasWordBreak-BreakAll.com</a> facere mollitia repellat hic, officiis, consequatur quam ad cumque dolorem doloribus ex magni necessitatibus, nisi accusantium.</p>
      
      <p>Voluptatibus atque inventore <a href="">http://www.looooooooooooooooooooooooooooongURL.com</a> veritatis exercitationem nihil minus omnis, quisquam earum ipsum magnam. Animi ad autem quos rem provident minus officia vel beatae fugiat quasi, dignissimos
        sapiente sint quam voluptas repellat!</p>
    </div>

    请确保仅将其应用于链接。否则,所有其他单词也会中断。 :)

    【讨论】:

      【解决方案2】:

      尝试使用white-space CSS 属性。

      查看此链接了解更多信息:http://perishablepress.com/press/2010/06/01/wrapping-content/

      【讨论】:

      【解决方案3】:

      我相信您正在寻找的是软连字符。不久前,ALA 对此进行了报道。

      http://www.alistapart.com/articles/the-look-that-says-book/

      【讨论】:

        【解决方案4】:

        我知道这是旧帖子,但由于我最终在这里进行了相关的谷歌搜索 - 其他人也可能这样做。

        我需要制作一个可破坏的长网址,因为它不适用于我的网站(即不在 html5 标准中,ie8 似乎忽略了它)

        <style>.wbr { display: inline-block; width: 0px;}</style>
        

        由于我生成了网址,因此我可以在斜杠之前插入 &lt;span class="wbr"&gt;&amp;nbsp;&lt;/span&gt;

        所以我最终得到了:

        www.example.com<span class="wbr">&nbsp;</span>/somepath<span class="wbr">&nbsp;</span>/blah.php
        

        在浏览器中看起来很正常,但允许在 / 标记处换行

        www.example.com/somepath/blah.php
        

        希望对下一位访问的人有所帮助

        【讨论】:

        • 谢谢特雷!它帮助了我的问题。我使用稍加修改的版本:我将 & 替换为 & 我没有放置  因为当用户复制 URL 并将其粘贴到他喜欢的任何位置时,我不希望它出现在 URL 中。
        猜你喜欢
        • 2017-04-07
        • 1970-01-01
        • 2013-05-27
        • 2010-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-17
        相关资源
        最近更新 更多