查找 div 元素中的所有链接并将其全部禁用

2022-01-20 00:00:00 find hyperlink jquery html disable-link

假设我有一些这样的 HTML 元素:

<div id="content_div"><span><a href="some_link">点我</a></span>

大家好.点击<a href="some_link_else">我</a>做别的事</div><a href="3rd_link">哎呀</a></div>

我只需要在#content_div 中获取所有a"标签并禁用所有标签(我不希望用户点击它们).我怎么能在 Jquery 中做到这一点?

解决方案

试试这个:

 $("#content_div a").click(function(e) {e.preventDefault();});

Assume that I have some HTML elements like these :

<div id="content_div">
    <span><a href="some_link">Click me</a></span>
    <div> Hello everybody. Click <a href="some_link_else">me</a> to do something else</div>

    <a href="3rd_link"> Oops </a>
</div>

All that I need is get all "a" tags in the #content_div and disable all of them (I don't want user to click on them). How could I do that in Jquery?

解决方案

Try this:

 $("#content_div a").click(function(e) {
   e.preventDefault();
 });

相关文章