简体   繁体   English

在textarea中的链接下删除新行

[英]Removing new line under link in textarea

When I create a new link for my textarea it creates a new line like 当我为textarea创建一个新链接时,它会创建一个新行,例如

[enter link description here][1] and [enter link description here][2]

   [1]: http://

   [2]: http://

As you can see above there is a gap between the bottom two links I don't want the gap there 如您在上方看到的,底部两个链接之间有一个缝隙,我不希望有缝隙

It should look like 它看起来像

[enter link description here][1] and [enter link description here][2]

   [1]: http://
   [2]: http://

Question how to make it so when create new link that there are no gaps for the bottom links. 在创建新链接时,请问如何做到这一点,以使底部链接没有空隙。

Here is a codepen demo has been updated with working code 这是一个codepen演示 ,已经使用工作代码进行了更新

<script type="text/javascript">
$('#myLink').on('shown.bs.modal', function() {
    var textarea = document.getElementById("message");
    var len = textarea.value.length;
    var start = textarea.selectionStart;
    var end = textarea.selectionEnd;
    var selectedText = textarea.value.substring(start, end);
    $('#title').val(selectedText);
    $('#url').val('http://');
});  

$('#save').on('click', function(e) {
    var textarea = document.getElementById("message");
    var len = textarea.value.length;
    var start = textarea.selectionStart;
    var end = textarea.selectionEnd;
    var selectedText = textarea.value.substring(start, end);
    var counter = findAvailableNumber(textarea);

    if ($.trim($('#title').val()).length == 0){
        var replace_word = '[enter link description here]' + '[' + counter + ']';
    } else {
        var replace_word = '[' + $(this).val() + ']' + '[' + counter + ']';
    }

    var add_link = '\n\n' + '   [' + counter + ']: ' + $('#url').val();

    textarea.value = textarea.value.substring(0, start) + replace_word + textarea.value.substring(end,len) + add_link;
}); 

function findAvailableNumber(textarea){
    var number = 1;

    var a = textarea.value;

    if(a.indexOf('[1]') > -1){

        //Find lines with links

        var matches = a.match(/(^|\n)\s*\[\d+\]:/g);

        //Find corresponding numbers

        var usedNumbers = matches.map(function(match){
            return parseInt(match.match(/\d+/)[0]); }
        );

        //Find first unused number

        var number = 1;

        while(true){

            if(usedNumbers.indexOf(number) === -1){

                //Found unused number

                return number;
            }

            number++;
        }
    }

    return number;
}
</script>

I think your code at: 我认为您的代码位于:

var add_link = '\n\n' + '   [' + counter + ']: ' + $('#url').val();

should instead be 相反应该是

if (counter == 1)
    var add_link = '\n\n' + '   [' + counter + ']: ' + $('#url').val();
else
    var add_link = '\n' + '   [' + counter + ']: ' + $('#url').val();

so that there is no gap between links, except between the text and link. 因此,除了文本和链接之间,链接之间没有其他间隙。 Since the links should be next to each other (see assumption below), only the first link should have a gap 由于链接应彼此相邻(请参见下面的假设),因此仅第一个链接应留有空隙

ASSUMPTION : I'm assuming there shouldn't be any text after the links. 假设 :我假设链接不应有任何文字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM