【发布时间】:2018-09-23 15:43:25
【问题描述】:
我有以下代码输入用户的 链接 并使用 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。我不想要那些默认网址。我该怎么办?
【问题讨论】:
-
“链接包含一些默认的网站网址”,您能详细说明一下吗?
-
@Arvind 尝试输入任何网站链接,然后您会在控制台中输入链接之前看到一些默认网址。
标签: javascript html anchor