javascript - 从 anchor 标记中删除默认网址

标签 javascript html anchor

我有以下代码,它输入用户的链接,并使用href = link创建anchor标签

<html>
<head>
    <title>Page Title</title>
    <style>
        #text-input {
            border-left: 4px solid green;
            text-indent: 12px;
        }
    </style>
</head>
<body>
    <p>Enter a link</p>
    <div id="text-input" contenteditable="true"></div>
    
    <script>
      let div = document.getElementById('text-input');
      let anchor;

      div.addEventListener('blur', event => {
        let text = div.innerText;
        div.innerText = '';
        anchor = document.createElement('a');
        div.appendChild(anchor);
        anchor.innerText = text;
        anchor.href = text;
        
        // The below line logs the actual link of the anchor tag
        console.log(anchor.href);
      });
    </script>
</body>
</html>

问题

当我将 href 分配给链接的值时,该链接似乎也包含一些默认网站 url。我不想要那些默认网址。我该怎么办?

最佳答案

也许这是相对网址的问题?如果文本中没有“http://”,请在文本开头添加“http://”。

关于javascript - 从 anchor 标记中删除默认网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52463880/

相关文章:

javascript - 如何在按 Enter 之前检查焦点在哪里?

javascript - 绝对定位的 DIV 不可见

html - 如何让一个 child 以两个轴为中心,其他 child 围绕着它?

javascript - 如何链接到javascript中的同一页面 anchor

javascript - 在 Javascript 中的触发事件上添加 anchor 标记

ruby-on-rails - Link_to block 内的 Rails 简单 anchor

javascript - 复选框依赖 JavaScript

javascript - 如何将 "push notifications"发送到特定用户网页?

javascript - 在 jQuery 中调用 PHP 变量

css - 将 HTML 背景设置为两个三 Angular 形