2013-02-26 28 views
0

我想用JQuery编写一个简单的函数。我创造了这个jfiddle:JQuery - 添加内容,从wordpress主题独立

<table class="my_table"> 
    <tr> 
     <td class="correct"></td> 
     <td> 
      <input type="button" class="my_button" /> 
     </td> 
    </tr> 
    <tr> 
     <td class="incorrect"></td> 
     <td> 
      <input type="button" class="my_button" /> 
     </td> 
    </tr> 
... 

http://jsfiddle.net/Xz3xc/1/

点击后,一些内容被添加到上面的按钮。我的本地机器就是这种情况。我在我的WordPress运行的另一个主题和代码中有如下:

<table class="my_table"> 
    <tr> 
     <td class="correct"></td> 
     <td> 
      <div> 
      <input type="button" class="my_button" /> 
      </div> 
     </td> 
    </tr> 
     <tr> 
     <td class="correct"></td> 
     <td> 
      <div> 
      <input type="button" class="my_button" /> 
      </div> 
     </td> 
    </tr> 
... 

http://jsfiddle.net/KuACZ/1/

有,当然代码不工作,因为一个父()在功能缺失。我怎样才能编写这个函数通用的,它总是把第一个td放在同一个tr按钮被点击的地方,这样它就与主题无关。

谢谢!

+0

请问我你的答案帮助吗? – 2013-02-27 22:17:20

回答

2

这段代码找到所有td的类与correct在同一tr内。

$('.my_button').click(function() { 
    $(this).parentsUntil("tr").siblings("td.correct").html('Test'); 
}); 

jsFiddle