[英]Find parent() of control, then find(“a”) JQuery
I have a <td>
that looks something like this: 我有一个
<td>
,看起来像这样:
<td class="myTDClass">
<span class="spanClass">Header</span>
<br />
<img class='myImages' style='cursor: hand;' onclick='doSomething(control)' alt='this is my alt Text'></img>
<br />
<span style="color: #123456;">More Text</span>
<br />
<a class='findThisClass'>Insert Alt Text Here</a>
</td>
This calls some jQuery code that sets the alt tag to some new text. 这将调用一些jQuery代码,将alt标签设置为一些新文本。 Then, i want it to set the
<a>
element to some new text. 然后,我希望它将
<a>
元素设置为一些新文本。 Here is the jQuery code: 这是jQuery代码:
doSomething = function(control)
{
var $myControl = $(control);
$myControl.attr("alt", "This is the new Alt Text!");
var $newControl = $(control).parent().siblings().find(".findThisClass").first();
alert($newControl.find("a").text());
});
The jQuery code sets the alt tag great, but doesn't find the <a class=findThisClass />
. jQuery代码将alt标签设置得很好,但是没有找到
<a class=findThisClass />
。
I think I'm using the .parent()
and .siblings()
wrong. 我认为我在使用
.parent()
和.siblings()
错误。 Can anyone find my error? 谁能找到我的错误? The idea here is that I can search just inside the
<td>
for the <a>
element, and not touch the other elements in other <td>
s 这里的想法是,我可以搜索只是里面的
<td>
的<a>
元素,并没有触及在其他元素以外的<td>
小号
Try this instead: 尝试以下方法:
var $newControl = $(control).closest('.myTDClass').find(".findThisClass");
alert($newControl.text());
Also img
is self closing tag, instead of: 另外
img
是自动关闭标签,而不是:
<img></img>
Just use: 只需使用:
<img... />
And instead of: 而不是:
onclick='doSomething(control)'
Use: 采用:
onclick='doSomething(this)'
so that jQuery has really a control to work with :) 这样jQuery确实可以使用:)
Assuming you want to find the <a>
that's in the same table cell as your <img>
, you don't need to use parent()
. 假设您要查找与
<img>
在同一表单元格中的<a>
,则无需使用parent()
。 All you need to do is call .siblings
Try: 您需要做的就是致电
.siblings
尝试:
var $newControl = $(control).siblings('a.findThisClass').first();
This assumes control
points to the image within the table cell. 假定
control
点指向表格单元格中的图像。
$("a.findThisClass", $myControl).first()
a
是图像的同级,因此,只需:
$(control).siblings(".findThisClass")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.