选择第一个兄弟姐妹

发布于 2024-08-19 04:28:10 字数 1508 浏览 6 评论 0原文

我试图在无法更改 html 标记的环境中使用 jQuery 选择第一个同级的内部值。

我有以下内容:

<tr>
    <td>3</td>
    <td>bob</td>
    <td>smith</td>
    <td>[email protected]</td>
    <td>
        <img src="bobsmith.png" onclick="doSomething()" />
    </td>
</tr>

我正在尝试使用以下内容获取第一个 的值:

function doSomething() {
    var temp = $(this).parent().parent().children().filter(':first');
    alert("you clicked person #" + temp.html());
}

我从中得到的只是 null

我也尝试了与 .siblings() 函数的各种组合,但无济于事。

有什么想法吗?

谢谢,

注意:我忘了提及摘录的表格是动态加载的并且是动态加载的。从 ajax 调用刷新。这可能与包含绑定的建议有关。

解决方案: 受到公认答案的启发,我采用了以下解决方案:

<tr>
    <td>3</td>
    <td>bob</td>
    <td>smith</td>
    <td>[email protected]</td>
    <td>
        <img src="bobsmith.png" onclick="doSomething(this)" />
    </td>
</tr>

对于 jQuery javascript:

function startStopNode(el) {
    var temp = $(el).parent().siblings(':first').html();
    alert("you clicked: " + temp);
}

I'm trying to select the inner value of the first sibling - using jQuery - in an environment where I cannot alter the html markup.

I have the following:

<tr>
    <td>3</td>
    <td>bob</td>
    <td>smith</td>
    <td>[email protected]</td>
    <td>
        <img src="bobsmith.png" onclick="doSomething()" />
    </td>
</tr>

I'm trying to get the value of the first <td> with the following:

function doSomething() {
    var temp = $(this).parent().parent().children().filter(':first');
    alert("you clicked person #" + temp.html());
}

All I get from this is null.

I've tried various combinations with with the .siblings() function too, but to no avail.

Any ideas?

Thanks,

Note: I forgot to mention that the table the excerpt is from is dynamically loaded & refreshed from an ajax call. This may be pertinent for suggestions that include binds.

Solution:
I've gone with the following solution, inspired by the accepted answer:

<tr>
    <td>3</td>
    <td>bob</td>
    <td>smith</td>
    <td>[email protected]</td>
    <td>
        <img src="bobsmith.png" onclick="doSomething(this)" />
    </td>
</tr>

and for the jQuery javascript:

function startStopNode(el) {
    var temp = $(el).parent().siblings(':first').html();
    alert("you clicked: " + temp);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

最丧也最甜 2024-08-26 04:28:10
$( 'td:first-child', $( this ).parents ( 'tr' ) ).html ();

这将选择图像的父 TR 中的第一个 TD 元素(:first-child 过滤器)。 parents() 返回元素的所有父元素,我们过滤父元素以便仅返回 TR 元素。

还可以尝试像这样编写您的图像:

<img src="bobsmith.png" onclick="doSomething(this)" />

以及像这样编写您的函数:

function doSomething ( imgEl ) {
}

并使用 imgEl 而不是 this

$( 'td:first-child', $( this ).parents ( 'tr' ) ).html ();

This will select the first TD element (:first-child filter) in the parent TR of the image. parents() returns all parents of the element, and we filter the parents so that only TR elements are returned.

Also try writing your image like so:

<img src="bobsmith.png" onclick="doSomething(this)" />

and your function like so:

function doSomething ( imgEl ) {
}

and use imgEl instead of this

我不在是我 2024-08-26 04:28:10
$('tr td:last-child img').click(function(){

    alert($('td:first-child',$(this).closest('tr')).text());

});
$('tr td:last-child img').click(function(){

    alert($('td:first-child',$(this).closest('tr')).text());

});
她说她爱他 2024-08-26 04:28:10

我会使用 jQuery 设置一个事件,但这完全取决于你。

$("table td:last img").click(function() {
    var firstTd = $(this).parents("tr").find("td:first").html();
    alert("you clicked person #" + firstTd);
}); 

无论如何,您仍然可以在自己的代码中使用此示例:

function doSomething()
{
    var firstTd = $(this).parents("tr").find("td:first-child").html();
    alert("you clicked person #" + firstTd);
}

您是对的。 'this' 没有被传递,您需要编辑 html 才能使其工作。或者只是使用 jQuery,如上所示。

I would set up an event using jQuery, but that's totally up to you.

$("table td:last img").click(function() {
    var firstTd = $(this).parents("tr").find("td:first").html();
    alert("you clicked person #" + firstTd);
}); 

No matter what, you can still use this example with your own code:

function doSomething()
{
    var firstTd = $(this).parents("tr").find("td:first-child").html();
    alert("you clicked person #" + firstTd);
}

You're right. 'this' is not being passed, you need to edit the html to make that work. Or just use jQuery instead as show above.

自找没趣 2024-08-26 04:28:10

也许这会对某人有所帮助。这只是此处整篇文章的一部分。

区别

如果您想使用 this 来访问正在处理该事件的 HTML 元素,则必须确保 this 关键字实际上已写入 onclick 属性中。仅在这种情况下,它才会引用事件处理程序注册到的 HTML 元素。因此,如果您这样做,

element.onclick = doSomething;
alert(element.onclick)

function doSomething()
{
    this.style.color = '#cc0000';
}

会看到,this 关键字出现在 onclick 方法中。因此它指的是 HTML 元素。

但如果你这样做,

<element onclick="doSomething()">
alert(element.onclick)

你会得到

function onclick()
{
    doSomething()
}

This is just a reference to function doSomething()。 onclick 方法中不存在 this 关键字,因此它不引用 HTML 元素。

maybe this will help somebody. This is just a part of the whole article here.

The difference

If you want to use this for accessing the HTML element that is handling the event, you must make sure that the this keyword is actually written into the onclick property. Only in that case does it refer to the HTML element the event handler is registered to. So if you do

element.onclick = doSomething;
alert(element.onclick)

you get

function doSomething()
{
    this.style.color = '#cc0000';
}

As you can see, the this keyword is present in the onclick method. Therefore it refers to the HTML element.

But if you do

<element onclick="doSomething()">
alert(element.onclick)

you get

function onclick()
{
    doSomething()
}

This is merely a reference to function doSomething(). The this keyword is not present in the onclick method so it doesn't refer to the HTML element.

笨死的猪 2024-08-26 04:28:10

我们有一个表行,其中一列是操作,其他列包含数据。其中一列包含产品代码 (UPC)。因此,每当有人单击操作按钮时,我们就使用它来显示产品代码:

var product_code = jQuery(obj).parents('tr').children('.upc').text();
alert('product code'+product_code);

这是有效的,因为我们的表格单元格都有类,例如每行都有 1919191911919。

不过,您可以使用 jQuery 中的其他选择器。例如 .children('#myid') 如果您在单元格 19191919199191 上设置了 id 属性,以及任意数量的其他选择器(如 .children(td:first) )以获取同一行上的第一个单元格。

We have a table row where one column is the action, other columns contain data. One of the columns contains the product code (UPC). So we use this to display the product code whenever someone clicks an action button:

var product_code = jQuery(obj).parents('tr').children('.upc').text();
alert('product code'+product_code);

This works because our table cells have classes, such as 1919191911919 for every row.

You can use other selectors from jQuery, however. Such as .children('#myid') if you have id attributes set on the cell 19191919199191 as well as any number of the other selectors like .children(td:first) to get the first cell on the same row.

谁与争疯 2024-08-26 04:28:10

真的,在这个用例中不需要 jquery。因此,对于那些浏览一般答案而不是专门针对 jquery 的人,或者那些愿意寻求更高性能解决方案的人来说。该解决方案比 jquery 对应方案快大约 10 倍。 查看参考

function doSomething(el) {
    var temp = el.closest('tr').firstElementChild.innerHTML;
    alert("you clicked: " + temp);
}

REALLY, no need for jquery in this use case. So for those browsing for a general answer and not specifically to jquery, or those willing for a more performant solution. This solution is approx 10x faster than its jquery counterpart. See reference

function doSomething(el) {
    var temp = el.closest('tr').firstElementChild.innerHTML;
    alert("you clicked: " + temp);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文