简体   繁体   English

查找文本<p> div HTML/Jquery 中最近的一行

[英]Finding text of <p> closest row inside a div HTML/Jquery

My table is built with the same ID's on every row, with a foreach loop.我的表在每一行上都使用相同的 ID 构建,并带有 foreach 循环。 To find the text inside the <p class="comment" - I have to specify where ( I want to find the closest one )要在<p class="comment"找到文本 - 我必须指定在哪里(我想找到最接近的)

My table looks like this:我的桌子看起来像这样:

        <c:forEach items="${items}" var="item">
        <div class="container theme-showcase" role="main">
            <div class="row">
                <div class="col-md-6">
                    <table class="table fixed">
                        <tbody id="extend">
                            <tr data-toggle="collapse123" class="clickableRow" nowrap="true" data-target=".demo${item.id}" id="${item.id}">
                                <td class="AMLLeft" nowrap="true">
                                    <label for="important"style="display: inline !important">ID:</label>
                                    <p class="important"style="display: inline !important">${item.id}</p>
                                </td>
                                <td align="right" class="AMLRight">
                                    <pre class="thepre">Transaction sum: ${item.sum} ${item.currencyCode}</pre>
                                </td>
                            </tr>
                            <tr class="hiddenRow">
                                <td align="left">
                                    <div class="collapse123 demo${item.id}"><p>AML ID: ${item.id}</p></div>
                                </td>
                                <td align="right" nowrap="false">
                                    <div class="collapse123 demo${item.id}"><input type="button" value="Comment" class="btn btn-xs btn-primary commentButton" <a href="#myModal" data-toggle="modal" id="${item.id}" data-target="#edit-modal"></div>
                                </td>
                            </tr>
                            <tr class="hiddenRow">
                                <td align="left">
                                    <div class="collapse123 demo${item.id}"><p>AML Category: ${item.amlClientCategory}</p></div>
                                </td>
                                <td align="right">
                                    <div class="collapse123 demo${item.id}"><input type="button" value="Close" class="btn btn-xs btn-primary closeButton"></div>
                                </td>
                            </tr>
                            <tr class="hiddenRow">
                                <td align="left">
                                    <div class="collapse123 demo${item.id}"><p>Client ID: ${item.clientId}</p></div>
                                </td>
                                <td align="right">
                                    <div class="collapse123 demo${item.id}"><input type="button" value="Set Investigate" class="btn btn-xs btn-primary investigateButton"></div>
                                </td>
                            </tr>
                            <tr class="hiddenRow">
                                <td align="left">
                                    <div class="collapse123 demo${item.id}"><p class="status">Status: ${item.status}</p></div>
                                </td>
                                <td align="right">
                                    <div class="collapse123 demo${item.id}"></div>
                                </td>
                            </tr>
                            <tr class="hiddenRow">
                                <td align="left">
                                    <div class="collapse123 demo${item.id}"><p class="comment">Comment: ${item.comment}</p></div>
                                </td>
                                <td align="right">
                                    <div class="collapse123 demo${item.id}"></div>
                                </td>
                            </tr>
                            <tr class="hiddenRow">
                                <td align="left">
                                    <div class="collapse123 demo${item.id}"><p>Transaction sum: ${item.sum} ${item.currencyCode}</p></div>
                                </td>
                                <td align="right">
                                    <div class="collapse123 demo${item.id}"></div>
                                </td>
                            </tr>
                    </table>

                </div>
            </div>
        </div>
    </c:forEach>

I want to find the text of <p class="comment">${item.comment}</p> but this time it's inside a <div> .我想找到<p class="comment">${item.comment}</p>的文本,但这次它在<div> I'm not sure how to do this but this is the code I've tried so far:我不知道该怎么做,但这是我迄今为止尝试过的代码:

var commentValue = $(this).closest('tr.hiddenRow').siblings().find('p.comment').text();

Any help is very appreciated!非常感谢任何帮助!

I know I've posted a similar question here but this is different because it's inside a <div> , and I can't help myself.我知道我在这里发布了一个类似的问题但这是不同的,因为它在<div> ,我无法帮助自己。

To save people trawling through the comments, the eventual solution is here: https://jsfiddle.net/5nL1e31z/3为了避免人们浏览评论,最终的解决方案在这里: https : //jsfiddle.net/5nL1e31z/3


I believe this achieves what you wanted, and I've tried to break it out so it's clear what's happening: https://jsfiddle.net/5nL1e31z/ If it isn't quite right, just let me know and I can update it.我相信这可以达到您想要的效果,并且我已经尝试将其分解,因此很清楚发生了什么: https : //jsfiddle.net/5nL1e31z/如果它不太正确,请告诉我,我可以更新它.

$('.test-button').click(function () {

    var parentRow = $(this).parent().parent();
    $(parentRow).css('background-color', 'red');

    var hiddenRow = $(parentRow).siblings('.hiddenRow');
    $(hiddenRow).css('background-color', 'yellow');

    var commentTextP = $(hiddenRow).find('.comment');
    console.log(commentTextP.text());
});
var commentvalue = $(this).parent().find('p').text();

http://jsfiddle.net/ux0a3zsx/ http://jsfiddle.net/ux0a3zsx/

Because the button is what this refers to and p is not inside it因为buttonthis所指的,而p不在里面

你试过这个吗?

var text= $(this).closest('.hiddenRow').find('.comment');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM