0

我有这个不起作用的代码

首先我添加一个img div

$('div.group_title a').prepend("<img src='replace_w_img_link_in_real'  width='20' height='20'  >");

然后我选择那个 div

$('.el.group_title a img').on('click', function(e){
  e.preventDefault();
  var img2 = "img_link_path_2";
  var img1 = "img_link_path_1";

  var img = $(this).attr("src");

  if (img == img1) {
    $(this).attr("src",img2);
  } else {
    $(this).attr("src",img1);
  }

  $(this).nextUntil('.el.group_title', '.sidebar_project').toggle();
});

这里的问题是$('.el.group_title a img')只返回一个选择,当我这样做时我$(this).nextUntil('.el.group_title', '.sidebar_project').toggle(); 找不到 div。

所以想法是它添加了一个 img div,当您单击该 img 时,img 会更改并隐藏/显示内容.sidebar_project,直到下一个 img。我试图改变$(this).nextUntil('.el.group_title a img'但没有运气

根据要求,这是基本 html

<div class="el group_title">
  <a href="link_path">NAME</a>
</div>
<div data-project-id="195677" class="sidebar_project">..</div>
<div data-project-id="9844" class="sidebar_project">..</div>
<div class="el group_title">
  <a href="link_path">NAME 2</a>
</div>
<div data-project-id="195677" class="sidebar_project">..</div>
<div data-project-id="9844" class="sidebar_project">..</div>

现在当我添加 img div

<div class="el group_title">
  <a href="link">
    <img alt="ACME org" src="link" width="20" height="20">org name</a>
</div>
....

这是 jsFiddle -> http://jsfiddle.net/6fdMc/

4

1 回答 1

0

It's hard to tell exactly what you're trying to do, but it sounds like part of the solution will be to $( this ).closest( ".group_title" ). Are you trying to do something like this? This is the part that I changed in your code:

Original:

$(this).nextUntil('.el.group_title', '.sidebar_project').toggle();

New:

$( this ).closest( ".group_title" )

  .nextUntil( ".group_title", ".sidebar_project" ).toggle();
于 2012-08-23T11:34:41.777 回答