【问题标题】:How do I re-enable links with jQuery that have been disabled with e.preventDefault()?如何重新启用已被 e.preventDefault() 禁用的 jQuery 链接?
【发布时间】:2018-09-20 05:20:14
【问题描述】:

我正在努力在一组嵌套的<ul> 中使用一些 jQuery 和 CSS 来制作一个长链接页面,这样对用户更加友好。我无法直接编辑 HTML,但我能够编写代码来折叠列表,以便只显示顶级 <li>,然后当用户单击一个时,它会展开以显示所有子项。

问题是顶级项目也是链接。我想在<li> 折叠时禁用链接(并且具有类 .notActive),并在展开时重新启用它们(并且具有类 .open),以便用户可以展开菜单而不会离开页面。

不幸的是,我尝试过的解决方案要么 100% 禁用链接(使用 event.preventDefault(); 时),要么禁用所有点击事件(使用 pointer-events:none; 时),这也禁用了展开/折叠功能.

我读过类似的问题,比如这个: How do I dynamically enable/disable links with jQuery? 但还没有弄清楚如何重新启用链接。

这是我的 jsfiddlehttps://jsfiddle.net/9raufx3s/121/,这是我的代码目前的样子:

代码:

$(document).ready(function($) {

	$(".children").closest("li").addClass("hasSubmenu");

	if ($("li").hasClass("hasSubmenu")) {
		$("li.hasSubmenu").append("<span class='plus'>&nbsp;&nbsp;&#43;</span>");
	}

	$(".cat-item").click(function(e) {
		$(this).toggleClass("open");
		e.stopPropagation();
	});
});

$("ul.category-column").after("<p class='all'>Expand All</p>");
$("p.all").click(function(f) {
	$(".cat-item").addClass("open");
});

$(document).ready(function($) {
	var top = $("ul.category-column li.hasSubmenu").first();
	top.addClass("topLevel notActive");
	top.siblings("li").addClass("topLevel notActive");

	$("li.topLevel").click(function(n) {
		$(this).toggleClass("notActive");
	});
});
$(document).ready(function($) {
	$("li.notActive a").on('click', function(disabled) {
		disabled.preventDefault();
	});
	$("li.open a").on('click', function(enabled) {
		return true;
	});
});
li.open * {
  display:block !important;
}    
.children {
  display:none;
}    
li.open>span.plus {
  display:none !important;
}    
li.open>ul.children>li.hasSubmenu>span.plus {
  display:none !important;
}    
.all {
  font-weight:bold;
  cursor:pointer;
}    
.notActive {
  color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="category-column">
	<li class="cat-item cat-item-3"><a href="https://www.google.com">I. Heading Number One</a>
		<ul class="children">
			<li class="cat-item cat-item-481"><a href="#">Subheading A</a></li>
			<li class="cat-item cat-item-483"><a href="https://www.google.com">Subheading B</a>
				<ul class="children">
					<li class="cat-item cat-item-587">Child of subheading B</li>
					<li class="cat-item cat-item-588">Second child of subheading B</li>
				</ul>
			</li>
		</ul>
	</li>

	<li class="cat-item cat-item-4">II.Heading Number Two
		<ul class="children">
			<li class="cat-item cat-item-200"><a href="#">Subheading C</a></li>
			<li class="cat-item cat-item-201"><a href="#">Subheading D</a>
				<ul class="children">
					<li class="cat-item cat-item-300">Child of subheading D</li>
					<li class="cat-item cat-item-301">Second Child of subheading D</li>
				</ul>
			</li>
		</ul>
	</li>
</ul>

【问题讨论】:

标签: javascript jquery html css


【解决方案1】:

要回答您关于撤消 preventDefault 的问题 - 您可以使用事件绑定和取消绑定,作为简化展示:

function generic(evt) {
  console.log('generic');
  $('.toggable', this).toggleClass('hidden');
};

function toggle(evt) {
  console.log('prevents');
  evt.stopPropagation();
  evt.preventDefault();
}

$('.toggle').on('click', function() {
  var $this = $(this),
      $target = $('.target');
  if($this.hasClass('prevents')) {
    $target.unbind('click', toggle);
    $this.removeClass('prevents');
  } else {
    $target.bind('click', toggle);
    $this.addClass('prevents');
  }
});

$('.target').bind('click', generic);
.hidden { display: none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="https://duckduckgo.com/" class="target" target="_blank">Duck<span class="toggable hidden">(toggable)</span></a>

<hr>

<a href="#" class="toggle">Toggle handler</a>

但是对于您的用例,您可以以更简单、更有效的方式来完成它,再次作为一个简化的展示:

$('li').each(function() {
  var $this = $(this),
      $children = $('>ul', this),
      $link = $('>a', this);
  $this.on('click', function(evt) {
    console.log('li clicked, propagation stopped');
    // Don't bubble up collapsing
    evt.stopPropagation();
    $children.toggleClass('open');
  });
  $link.on('click', function(evt) {
    if($children.length === 0 || $children.hasClass('open')) {
      // If children are expanded or there are no children
      // only prevent bubbling up the event
      console.log('link clicked, propagation stopped');
      evt.stopPropagation();
    } else {
      // If children are not expanded yet
      console.log('link clicked, default prevented');
      evt.preventDefault();
    }
  });
});

var collapsed = true;
$('.toggle-all').on('click', function(evt) {
  evt.stopPropagation();
  evt.preventDefault();
  if(collapsed) {
    $('ul').addClass('open');
    collapsed = false;
  } else {
    $('ul').removeClass('open');
    collapsed = true;
  }
});
li>ul { display: none; }
li>ul.open { display: block; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>Parent
    <ul>
      <li>Child</li>
      <li>Child</li>
    </ul>
  </li>
  <li><a href="#">Parent</a>
    <ul>
      <li><a href="#">Child</a></li>
      <li>Child</li>
    </ul>
  </li>
</ul>

<a href="#" class="toggle-all">Toggle all</a>

【讨论】:

  • 谢谢,这几乎可以工作了!我仍然遇到的唯一问题是我的网站上有第三级儿童,他们没有扩大。当您单击其父 li 时,它只会将所有内容折叠回去,而不是展开下一级子级。我会自己处理,但如果您对如何让所有后代展开/折叠有任何建议,我们将不胜感激!编辑:没关系,它有效!
猜你喜欢
  • 2015-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-01
  • 2011-04-16
  • 2010-11-13
  • 1970-01-01
相关资源
最近更新 更多