我有以下代码,它输入用户的链接,并使用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/