[英]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.