Javascript 从锚标记中删除默认url

Javascript 从锚标记中删除默认url,javascript,html,anchor,Javascript,Html,Anchor,我有下面的代码,它从用户那里输入一个链接,并创建一个锚定标记,带有href=link 页面标题 #文本输入{ 左边框:4倍纯绿; 文本缩进:12px; } 输入链接 让div=document.getElementById('text-input'); 抛锚; div.addEventListener('blur',事件=>{ 让text=div.innerText; div.innerText=''; anchor=document.createElement('a'); 儿童组(锚);

我有下面的代码,它从用户那里输入一个链接,并创建一个
锚定
标记,带有
href=link


页面标题
#文本输入{
左边框:4倍纯绿;
文本缩进:12px;
}
输入链接

让div=document.getElementById('text-input'); 抛锚; div.addEventListener('blur',事件=>{ 让text=div.innerText; div.innerText=''; anchor=document.createElement('a'); 儿童组(锚); anchor.innerText=文本; anchor.href=文本; //下一行记录锚定标记的实际链接 console.log(anchor.href); });
也许这是一个相对URL的问题?如果文本没有“http://”,则在文本开头添加“http://”。

可能是相对URL的问题?如果文本没有“http://”,则在文本开头添加“http://”。

根据您的默认要求,您可以将https或http附加到输入数据

 anchor.innerText = text;       
            if(text.indexOf('http://')!=0 || text.indexOf('https://')!=0){
                text = "http://"+text;  //default http(or) https
            }
             anchor.href = text;

根据您的默认要求,您可以将https或http附加到输入数据

 anchor.innerText = text;       
            if(text.indexOf('http://')!=0 || text.indexOf('https://')!=0){
                text = "http://"+text;  //default http(or) https
            }
             anchor.href = text;

您可能需要在开头添加http://或https://


页面标题
#文本输入{
左边框:4倍纯绿;
文本缩进:12px;
}
输入链接

让div=document.getElementById('text-input'); 抛锚; div.addEventListener('blur',事件=>{ 让text=div.innerText; div.innerText=''; anchor=document.createElement('a'); 儿童组(锚); anchor.innerText=文本; anchor.href='http://'+文本; //下一行记录锚定标记的实际链接 console.log(anchor.href); });
您可能需要在开头添加http://或https://


页面标题
#文本输入{
左边框:4倍纯绿;
文本缩进:12px;
}
输入链接

让div=document.getElementById('text-input'); 抛锚; div.addEventListener('blur',事件=>{ 让text=div.innerText; div.innerText=''; anchor=document.createElement('a'); 儿童组(锚); anchor.innerText=文本; anchor.href='http://'+文本; //下一行记录锚定标记的实际链接 console.log(anchor.href); });
“链接包含一些默认网站url”,您能详细说明吗?@Arvind尝试输入任何网站链接,然后在控制台中输入链接之前,您会看到一些默认url。“链接包含一些默认网站url”,您能详细说明吗?@Arvind尝试输入任何网站链接,然后,您将在控制台中输入链接之前看到一些默认url。