简体   繁体   English

将表结果迭代到具有相同类的不同div

[英]Iterate Table result to different divs with the same class

Using this code....trying to get the result from each multiple table and target the result to a div that has the same class...one result from each table to each div....without adding any id's etc....and not using .after(). 使用此代码....尝试从每个多个表中获取结果并将结果定位到具有相同类的div ...从每个表到每个div中的一个结果....而不添加任何id等。 ..而不使用.after()。 Hope that makes sense. 希望有道理。 You can see some console.logs I have set up...but can't seem to get it to populate the divs with the correct results. 您可以看到我设置的一些console.logs ...但是似乎无法用正确的结果填充div。

<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
        <script type="text/javascript">
$(document).ready(function() {

//Get Number of Divs with class='sum'
var divN = $( ".sum" ).length;
console.log('Number of Divs in page that match class sum : ' + divN);

//Get Number of TDs with class='two'
var tdN = $( ".two" ).length;
console.log('Number of TDs in page that match class two : ' + tdN);

//Get Number of Tables
var tblN = $( "table" ).length;
console.log('Number of Tables in page : ' + tblN);

//Get Number of TDs with class='two' in each table
$("table").each(function(){
tblSum = $('.two', this).length;
console.log('Number of TDs that match class two in each Table : ' + tblSum);
});

//Get result from each table and place it in div class="sum"
$(".sum").text(tblSum);
});

        </script>
    </head>
    <body>
<table>
        <tr><td class="one">Test</td></tr>
        <tr><td class="one">Test</td></tr>
        <tr><td class="one two">Test</td></tr>
        <tr><td class="one">Test</td></tr>
        <tr><td class="one">Test</td></tr>
        <tr><td class="one two">Test</td></tr>

</table>
<div class="sum"></div>
<table>
        <tr><td class="one">Test</td></tr>
        <tr><td class="one">Test</td></tr>
        <tr><td class="one two">Test</td></tr>
        <tr><td class="one">Test</td></tr>
        <tr><td class="one">Test</td></tr>
        <tr><td class="one">Test</td></tr>

</table>
<div class="sum"></div>

</body>
</html>

You could use the next() function like : 您可以使用next()函数,例如:

$(this).next('.sum').text(tblSum);

 $(document).ready(function() { $("table").each(function() { var tblSum = $('.two', this).length; $(this).next('.sum').text(tblSum); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td class="one">Test</td> </tr> <tr> <td class="one">Test</td> </tr> <tr> <td class="one two">Test</td> </tr> <tr> <td class="one">Test</td> </tr> <tr> <td class="one">Test</td> </tr> <tr> <td class="one two">Test</td> </tr> </table> <div class="sum"></div> <table> <tr> <td class="one">Test</td> </tr> <tr> <td class="one">Test</td> </tr> <tr> <td class="one two">Test</td> </tr> <tr> <td class="one">Test</td> </tr> <tr> <td class="one">Test</td> </tr> <tr> <td class="one">Test</td> </tr> </table> <div class="sum"></div> 

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

相关问题 使用 Javascript 遍历多个父 div(具有相同的类)以访问内部的子 div(相同的类) - Iterate through multiple parent divs (with same class) to access the child divs inside (same class) using Javascript 如何遍历(几乎)同一类的多个div - How to iterate through multiple divs of (almost) same class 在三个不同的节/ div之间共享相同的ajax结果 - Share the same ajax result between three different sections/divs 如何对具有相同类的不同div使用相同的JQuery效果 - How to use the same JQuery effect for different divs with same class 在同一个类别中显示来自不同按钮的不同div - Show different divs from different buttons with same class 使用jQuery将悬停效果添加到同一类的不同div中的特定元素 - Add hover effect to specific elements in different divs of same class with jQuery 如何从同一类的不同div中选择元素? - how to select element from different divs of the same class? 如何使用相同的 class 在三个 div 中添加不同的图像 - How to add different images in three divs using the same class Javascript在具有相同类但不同变量的多个div中运行函数 - Javascript run function inside multiple divs with the same class but different variables 在共享相同 class 的多个不同 div 中更改颜色 - Change color in a number of different divs that all share the same class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM