js 获取url里面的域名与a连接写活

先说一下js获取url里面的域名地址
window.location.protocol+"//"+window.location.host;
这部分运行出来显示的是域名

这里是a连接里面的元素
<li>
   <a id="cont" href="" target="_blank"></a>
</li>
<li>
   <a id="hot" href="" target="_blank"></a>
</li>
这一段显示的就是a连接写活的部分
<script>
//先定义获取域名
       var url= window.location.protocol+"//"+window.location.host;
//其次往a连接里面填写地址  url是获取到的域名   +号是拼接上后面的地址
       $("#cont").attr("href",url+'/index/index/cont.html');
       $("#hot").attr("href",url+'/index/index/hot.html');
</script>

不懂的留言问我哦

发布了34 篇原创文章 · 获赞 16 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/weixin_42786143/article/details/101683015