如果我有这个函数给了我那么多的div,我怎么能改变它得到这样的结果:01 02 03 04 05 06 07?
提前致谢
$(document.body).ready(function() {
$(document.body).append($(".slide"));
var n = $(".slide").length;
$("span").text("0" + n);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<span></span>
<div class=slide></div>
<div class=slide></div>
<div class=slide></div>
<div class=slide></div>
<div class=slide></div>
<div class=slide></div>
<div class=slide></div>
答案 0 :(得分:1)
$(document.body).ready(function() {
$( document.body ).append( $( "<div>" ) );
var n = $( "div" ).length;
for(var i=0; i<n; i++){
$("span" ).append( "0" + i+'\u00A0');
}
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span></span>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
&#13;
答案 1 :(得分:1)
window.onload=function(){
var divs=document.getElementsByTagName('div');
var span=document.getElementsByTagName('span');
var divsLength=divs.length;
var count=1;
for(var index=0;index<divsLength;index++)
{
var label=document.createElement('label');
label.innerText='0'+count;
span[0].append(label);span[0].append(" " );
count++;
}
}
&#13;
<span></span>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
&#13;
希望这有帮助
答案 2 :(得分:1)
https://jsfiddle.net/tsobott7/5/
$(document.body).ready(function() {
$( document.body ).append( $( "<div>" ) );
var n = $( "div" ).length;
var str = "";
for (var x = 1; x < n; x++) {
str = str + "\n0" + x;
}
$( "span" ).text(str);
})
你可以通过使用for循环来实现。
答案 3 :(得分:0)
使用此代码
$(document.body).ready(function() {
$( document.body ).append( $( "<div>7th</div>" ) );
$( "div" ).each(function( index ) {
( index + ": " + $( this ).text() );
});
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<span></span>
<div>1st</div>
<div>2nd</div>
<div>3rd</div>
<div>4th</div>
<div>5th</div>
<div>6th</div>
&#13;