javascript - 从锚标记中删除默认 url
问题描述
我有以下代码,它输入来自用户的链接并创建一个anchor
标签href = link
<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://”。
推荐阅读
- qgis - 如何将工具栏按钮连接到脚本以执行一些 python 操作
- c++ - UI 没有在 QT 设计器中更新
- jquery - 如何从链接属性接收数据
- mysql - Mysql 数据库大小增长超过 4 TB?Azure 最多支持 4 个
- r - 在r中的json字符串中获取所有可能的键/键路径
- piranha-cms - 从 Piranha CMS 中的图像文件名中删除 guid
- python - 为什么 set().union(*list1) 给了我一个列表中两个列表的并集?
- docusignapi - 无法将 docusign 与第三方应用程序集成
- git - 从远程仓库拉取时,GitLab 镜像功能不起作用
- html - 为什么我的溢出和文本溢出:省略号代码在 MS Edge 中不起作用?