简体   繁体   English

如何使用jQuery单独检索每个div的内容

[英]How can I retrieve the contents of each div individually using jQuery

I have the following html and I've been having trouble selecting the content I desire. 我有以下HTML,我一直无法选择我想要的内容。 I am using jQuery and the .children() does not get the text nodes. 我使用的是jQuery,而.children()没有得到文本节点。

<div id="hiddenMenus">

    <div id="planetGranite">
        <div class="menuItem">
            <div class="productId">1</div>
            <div class="productName">Child Admission</div>
        </div>
        <div class="menuItem">
            <div class="productId">2</div>
            <div class="productName">Adult Admission</div>
        </div>
        <div class="menuItem">
            <div class="productId">3</div>
            <div class="productName">Senior Admission</div>
        </div>
        <div class="menuItem">
            <div class="productId">4</div>
            <div class="productName">Student Admission</div>
        </div>

        <!-- Variable For Loop (Easier than Child-Nodes) -->
        <div class="totalMenuItems">4</div>
    </div>

</div><!-- End of #hiddenMenus -->

I want to loop through the 'planetGranite' div and select the contents of each 'productName' class, one-by-one, so that I can build an interactive menu. 我想循环遍历'planetGranite'div并逐个选择每个'productName'类的内容,以便我可以构建一个交互式菜单。 Here is an example of the output: 以下是输出示例:

<div class="product">
            <div class="productName">Child Admission</div>
            <div class="quantity"><input type="text" name="quantity" placeholder="2"  /></div>
            <button type="button" onclick="addItem('Child Admission')">Add Item</button>
</div><!-- End of .product -->

Here is what I currently have, however, I know I am far off because I am not sure exactly how to approach this problem. 这是我目前所拥有的,但是,我知道我很远,因为我不确定如何解决这个问题。 It must work with any html formatted this way. 它必须适用于任何以这种方式格式化的HTML。

function generateMenu(menuId) {
    // Get the Total Number of Menu Items
    var totalItems = $('#' + menuId + " .totalMenuItems" ).html();
    if (DEBUG && DEBUG_INPUT) alert("Menu: " + menuId + "\n"
                                    + "Items: " + totalItems + "\n");

    // Get a reference to the Menu
    var menu = $('#' + menuId);
    var menuItem = null;

    //alert(menu.html());
    if (DEBUG && DEBUG_GEN_MENU) alert("Number of Children: " + (menu.children('.menuItem').length));
    if (DEBUG && DEBUG_GEN_MENU) alert("Children: " + "\n"
                                        + "firstChild: "
                                        + menu.contents().filter(function() {
                                                                return this.nodeType === 3;
                                                                })
                                                                .wrap( '<div class="productName"></div>' )
                                                                .end());



    for (var i = 0; i < menu.childNodes.length; i++) {
        if (menu.childNodes[i].className == "menuItem") {

            menuItem = menu.childNodes[i];
            console.log("MenuItem");
            //processMenuItem(menuItem);
            break;
        }  
    }

I've read up on the documentation, but I am struggling to get the contains() method to work. 我已经阅读了文档,但我正在努力让contains()方法起作用。 Please help me find the simplest and fastest way to select the contents. 请帮我找到选择内容的最简单,最快捷的方法。 Thanks! 谢谢!

Your edit showed up just as I was about to post this. 您的编辑就像我即将发布的那样出现了。 Here is some code that will get the products in the correct order. 以下是一些能够以正确的顺序获得产品的代码。 You will probably need to modify it to use it in your function. 您可能需要修改它以在函数中使用它。

$('#planetGranite').find('.productName').each(function(){
    var title = $(this).text();
    console.log(title);
});

You need to fetch contents of each div with class name productName by using .each() function as:: 你需要取得与类名的每个格的内容productName使用.each()函数为::

$(".productName").each(function(){
    alert($(this).text());
});

For better reference with Demo use this JSfiddle Link 为了更好地参考Demo,请使用此JSfiddle Link

 $(document).ready(function() { var planet = $('#planetGranite'); planet.children('.menuItem').each(function(item) { var productId = $('.productId', this).text(); var productName = $('.productName', this).text(); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="hiddenMenus"> <div id="planetGranite"> <div class="menuItem"> <div class="productId">1</div> <div class="productName">Child Admission</div> </div> <div class="menuItem"> <div class="productId">2</div> <div class="productName">Adult Admission</div> </div> <div class="menuItem"> <div class="productId">3</div> <div class="productName">Senior Admission</div> </div> <div class="menuItem"> <div class="productId">4</div> <div class="productName">Student Admission</div> </div> <!-- Variable For Loop (Easier than Child-Nodes) --> <div class="totalMenuItems">4</div> </div> </div><!-- End of #hiddenMenus --> 

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

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