栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

a标签可以再嵌套a标签吗?为什么?如果不行,那又想要嵌套效果怎么解决呢?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

a标签可以再嵌套a标签吗?为什么?如果不行,那又想要嵌套效果怎么解决呢?

a标签不能嵌套a标签

<a href="https://www.baidu.com/" >    点击父级标签    <a href="https://www.baidu.com/" target="_blank" >点击子级标签</a></a>

结果在浏览器上解析为,嵌套失败:

<a href="https://www.baidu.com/" >点击父级标签</a><a href="https://www.baidu.com/" target="_blank" >点击子级标签</a>

那么实现嵌套效果(点击子标签时跳转,点击父标签时跳转):

1.和上面的布局一样,样式改变如下,给父元素加绝对定位:

.parent {    display: block;    position: absolute;    width: 200px;    height: 100px;    border: 1px solid blue;    text-align: center;    line-height: 100px;}.child {    color: red;}

2.中间加一层object标签如下(大部分浏览器支持,但是还是存在兼容性):

<a href="https://www.baidu.com/" >    点击父级标签    <object data="" type="">        <a href="https://www.baidu.com/" target="_blank" >点击子级标签</a>    </object></a>

3.还可以不用a标签(随便用什么标签,实现嵌套和跳转功能),加js如下:

<div  id="parent">  点击父级标签  <a href="https://www.baidu.com/" target="_blank" rel="noopener noreferrer"  id="child">点击子级标签</a></div><script>  window.onload = () => {      let parent = document.getElementById('parent');      let child = document.getElementById('child');      parent.onclick = () => {          alert("在本页跳转到百度");          window.location.href = 'https://www.baidu.com/';      };      child.onclick = (e) => {          // 阻止事件默认的向上冒泡行为          e.stopPropagation();          alert("打开新页面跳转到百度");      };  };</script>
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/402203.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号