0

I'm pretty new to jquery so am not too knowledgeable on it, I asked a question on using jquery to change the class of certain elements to specific classes if another certain element is present - JQuery - Change certain element classes to specific classes if certain other element is present

It seems that an event listener is needed for this, I have a basic understanding of event handlers but this is generally to do with actions such as click etc what I need however is one to Id if certain elements innerhtml has a text value (e.g. 'go') to run a function on near by elements (example below).

This is for a blog, which will use certain tags to change layout of posts, below there is some repeated html to illustrate how the targetting needs to work

classes are spans (using bootstrap)

html

<div class="container">

<div  class="span6">
</div>

<div class="span6">
<a class="taglink">text</a>
</div>

</div>

<div class="container">

<div class="span6">
</div>

<div class="span6">
<a class="taglink">text</a>
</div>

</div>

What I'd like is for a & b's and only a & b's classes to change from span 6 to 12 when the text inside one of the a classes 'taglink' that is within the same container div is equal to 'go'.

This is so that only 'go' tag will affect only 'a' and 'b' divs classes and only that of a and b within its own 'container' div.

4

1 回答 1

0

现在的第一件事是:

ID 必须是唯一的,这是不可协商的。

因此,我将做出以下假设:

  1. id=aor id=bor id=container(倍数)的每个实例实际上分别读取class=a, class=b, 和class=container
  2. 链接文本不会动态更改

  $(document).ready(function()
      $('div.container').each(function(){
          if ($(this).find('a.taglink').eq(0).text()=="go"){
              $(this).find('div.a').removeClass('span6').addClass('span12');
              $(this).find('div.b').removeClass('span6').addClass('span12');    
          }
      }
  });
于 2013-02-01T23:00:24.690 回答