[英]How to remove the anchor tag from parent class not from child class?
I am removing the anchor tag from the parent class which is working but it also removing from the child class.我正在从正在工作的父 class 中删除锚标签,但它也从子 class 中删除。 I need an anchor tag on the child class.
我需要子 class 上的锚标签。
Would you help me out?你能帮帮我吗?
$(function() { $('#left-side-menu li.menu-item-has-children a').contents().unwrap(); });
<nav id="left-side-menu"> <ul> <li><a href="">Test 1</a></li> <li class="menu-item-has-children"><a href="">Test 2</a> <ul> <li><a href="">Test 2-1</a></li> <li><a href="">Test 2-2</a></li> </ul> </li> <li><a href="">Test 3</a></li> </ul> </nav> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
Use Child Combinator for CSS Selectors将子组合器用于 CSS 选择器
Solution for your example您的示例的解决方案
replace #left-side-menu li.menu-item-has-children a
to #left-side-menu li.menu-item-has-children > a
to detect the tag anchor link which locate directly inside li.menu-item-has-children
element将
#left-side-menu li.menu-item-has-children a
替换为#left-side-menu li.menu-item-has-children > a
以检测直接位于li.menu-item-has-children
内部的标记锚链接li.menu-item-has-children
元素
final code最终代码
$(function() {
$('#left-side-menu li.menu-item-has-children > a').contents().unwrap();
});
You can simply do this with CSS.
您只需使用 CSS 即可完成此操作。 Hope works for you
希望对你有用
.disable { pointer-events: none; text-decoration: none; color: black; }
<nav id="left-side-menu"> <ul> <li><a href="">Test 1</a></li> <li><a class="disable" href="">Test 2</a> <ul> <li><a href="">Test 2-1</a></li> <li><a href="">Test 2-2</a></li> </ul> </li> <li><a href="">Test 3</a></li> </ul> </nav>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.