如何在当前选择器后获取元素

时间:2012-06-25 23:13:05

标签: jquery jquery-selectors

我的问题可能有点不清楚所以我会举个例子

    <table id="products">
        <tr id="P1" class="A1"><td>1x</td><td>Product 1</td><td>&euro; 9,50</td><td><a href="#">(x)</a></td></tr>
        <tr id="P2" class="A1"><td>1x</td><td>Product 2</td><td>&euro; 10,50</td><td><a href="#">(x)</a></td></tr>
    </table>

我想要完成的是以下功能

    $('#bestelling tr').each(function(index) {
        var naam = $(this + " td + td").text();
        var id = $(this).attr('id'); 
        alert(id + " " + naam);
    });

有谁知道我怎么能做到这一点?对不起我对这个问题的简要解释我的英语不太好。

2 个答案:

答案 0 :(得分:1)

$('#bestelling tr td:eq(1)').each(function(index) {
    var naam = $(this).text();
    var id = this.id; 
    alert(id + " " + naam);

    // The tr is:
    $(this).parent()
    // Or the DOM element:
    this.parentNode 
});

答案 1 :(得分:0)

here you go mate. Run the code below: 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>How to get element after current selector</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('table tr').click(function (e) {
                var naam = "";
                $(this).children().each(function () {
                    naam += $(this).html() + ' ';
                });

                alert(naam);
            });

        });
    </script>

</head>
<body>
<table id="products">
    <tr id="P1" class="A1">
        <td>1x</td>
        <td>Product 1</td>
        <td>&euro; 9,50</td>
        <td><a href="#">(x)</a></td>
    </tr>
    <tr id="P2" class="A1">
        <td>1x</td>
        <td>Product 2</td>
        <td>&euro; 10,50</td>
        <td><a href="#">(x)</a></td>
    </tr>
</table>

</body>
</html>
相关问题