简体   繁体   English

如何替换div中的数字?

[英]How to replace numbers in a div?

With the following code, I'm getting the values of "id"(almost 35), and then add 1 to each "id", so 1 will be 2 and so on. 使用以下代码,我得到“id”的值(差不多35),然后为每个“id”添加1,因此1将是2,依此类推。 Where I'm stock, it is on how to replace that id number in the html. 我在哪里库存,它是如何替换html中的id号码。

This is the code that use to get the values of each id, then I push them into an array, then I run another "for loop" to add 1 to each value, but I don't how to return them to the html. 这是用于获取每个id的值的代码,然后我将它们推入一个数组,然后我运行另一个“for循环”为每个值添加1,但我不知道如何将它们返回到html。

 var x = document.getElementsByClassName('p-divs'); var portfolio = new Array; for (var i = 0; i < x.length; i++) { var y = document.getElementsByClassName('p-divs')[i].getAttribute('id'); portfolio.push(y); } console.log(portfolio); var portfolio2 = new Array; for (var i = 0; i<portfolio.length; i++) { var newId; newId = parseInt(portfolio[i]) + 1; portfolio2.push(newId); } console.log(portfolio2); 
 <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 p-divs" id="1"> <div class="portfolio"> <center> <img src="images/pace.png" width="230" height="190" alt="" class="img-responsive"> </center> </div> </div> 

Since you're using jQuery library the code could be simple than what you've so far using .each() method : 由于您使用的是jQuery库,因此代码可能比您目前使用的.each()方法简单:

$('.p-divs').each(function(){
  $(this).attr('id', Number(this.id) + 1);
});

Or shorter using using .attr() method callback like : 或者使用.attr()方法回调更短,如:

$('.p-divs').attr('id', function(){
    return Number(this.id) + 1;
});

The more clear version could be : 更清晰的版本可能是:

$('.p-divs').each(function(){
  var current_id = Number(this.id); //Get current id
  var new_id = current_id + 1;  //Increment to define the new one

  $(this).attr('id', new_id); //Set the new_id to the current element 'id'
});

Hope this helps. 希望这可以帮助。

 $(function(){ $('.p-divs').attr('id', function(){ return Number(this.id) + 1; }); //Just for Debug console.log( $('body').html() ); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="p-divs" id="1"> <div class="portfolio"> <center>Image 1</center> </div> </div> <div class="p-divs" id="2"> <div class="portfolio"> <center>Image 2</center> </div> </div> <div class="p-divs" id="3"> <div class="portfolio"> <center>Image 3</center> </div> </div> <div class="p-divs" id="4"> <div class="portfolio"> <center>Image 4</center> </div> </div> 

Using native javascript, just use getattribute 's opposite: setAttribute 使用本机javascript,只需使用getattribute的对面: setAttribute

for (var i = 0; i < x.length; i++)
{
    var y = document.getElementsByClassName('p-divs')[i].getAttribute('id'); 
    y++;
    document.getElementsByClassName('p-divs')[i].setAttribute("id",y);
}
  var j = document.getElementsByClassName('p-divs');
    for (var i = 0; i < x.length; i++) {
      j[i].id = portfolio2[i];
    }  

Add this to the end of your code. 将其添加到代码的末尾。 Vanilla JS. 香草JS。

j will be an array of your divs, i will keep count of which div we're on, and we are simply accessing the "id" of each element in the "j" array and updating it to the corresponding value in your pre-populated "portfolio2" array. j将是你的div的数组,我将继续计算我们所在的div,我们只是访问“j”数组中每个元素的“id”并将其更新为前面的相应值填充“portfolio2”数组。

Hope this helps! 希望这可以帮助!

PS- I would also recommend that instead of using 'new Array' to instantiate your arrays, you use the array literal notation '[]'. PS-我还建议你不要使用'new Array'来实例化数组,而是使用数组文字符号'[]'。 This is more concise and also avoids needing to put (); 这更简洁,也避免了需要put(); after Array. 在阵列之后。

I'd suggest, assuming I'm not missing something, and that you're able to us ES6 methods: 我建议,假设我没有遗漏某些东西, 并且你能够使用ES6方法:

// converting the NodeList returned from document.querySelectorAll()
// into an Array, and iterating over that Array using
// Array.prototype.forEach():
Array.from( document.querySelectorAll('.p-divs') ).forEach(

  // using an Arrow function to work with the current element
  // (divElement) of the Array of elements,
  // here we use parseInt() to convert the id of the current
  // element into a number (with no sanity checking), adding 1
  // and assigning that result to be the new id:
  divElement => divElement.id = parseInt( divElement.id, 10 ) + 1
);

Note that updating, changing or otherwise modifying an id shouldn't be necessary in most circumstances, and having a purely numeric id may present problems for CSS selecting those elements (it's valid, but only in HTML 5, but will still be problematic). 请注意,在大多数情况下,更新,更改或以其他方式修改id不是必需的,并且具有纯数字id可能会给CSS选择这些元素带来问题(它是有效的,但仅在HTML 5中,但仍然存在问题)。

for(i=0;i<$('.p-divs').length;i++){
 newId= parseInt($($('.p-divs')[i]).attr('id'))+1;
 $($('.p-divs')[i]).attr('id',newId)
}

Using Jquery attr 使用Jquery attr

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

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