从链接开始修剪空格,并在A标记的外部添加空格



我的博客上有一个作者总是错误地在链接里面放空格,所以每个链接都以下划线开始。这很烦人。我试图通过jquery用下面的代码来解决这个问题,但似乎无法解除初始空间的链接,并在html 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/

如果我们去掉空格,单词和链接就会混在一起

使用.trim()删除空格,.before()<a>标签前添加空格

$(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>

最新更新