Trim spaces 从 link 开始,并在 A 标签外添加 space

Trim spaces from link beginning, and prepend space to outside of A tag

我的博客上有一位作者一直错误地将 space 放在 link 中,所以每个 link 都以带下划线的 space 开头。这很烦人。我尝试使用以下代码通过 jquery 修复此问题,但似乎无法取消 link 初始 space 并添加非 linked space 在 HMTL A 元素上方。

    text = $(this).text();
    if (text[0] == ' ') {
        console.log(this);
        $(this).text($.trim( $(this).text() ));
        // theHTML = $(this).outerHTML();
        $(this).outerHTML().replaceWith('=' + $(this).outerHTML());
        // $(this).prepend('=');
    }

示例:http://jsfiddle.net/691tx33w/

如果我们删除 space,单词和 link 就会混淆在一起。

使用 .trim() 删除 space 并使用 .before()<a> 标签前添加 space

$(document).ready(function() {
    $('a').each(function() {        
        if ($(this).text().charAt(0) == ' ') {
            $(this).text($.trim($(this).text()));
            $(this).attr('href', $.trim($(this).attr('href')));
            $(this).before(' ');
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>My wife and I live in southern Florida where, according to<a title="1.1 Million U.S. Properties with Foreclosure Filings in 2014, Down 18 Percent From 2013 to Lowest Level Since 2006" href="http://www.realtytrac.com/news/foreclosure-trends/1-1-million-u-s-properties-with-foreclosure-filings-in-2014-down-18-percent-from-2013-to-lowest-level-since-2006/" target="_blank"> one report</a>, 27% of homes have seen a foreclosure notice since 2007. In other places the numbers aren’t as bad, but they’re still depressing. What happened? Sure, times were tough, but even when<a title="unemployment topped 10%" href="http://www.nytimes.com/2009/11/07/business/economy/07jobs.html?_r=0" target="_blank"> unemployment topped 10%</a>, that left about 90% of us employed.</p>