简体   繁体   English

用js更改内部html id

[英]change a inner html id with js

how do you change a inner id with js and keep it the same id num (eg hey1, bob2) 如何使用js更改内部ID并保持相同的ID num(例如hey1,bob2)

my js code 我的js代码

var obj = document.getElementById("chat").cloneNode(true);
var obj1 = document.getElementById("ch");
var obj2 = document.getElementById("chatbox");

var p = $(".chat");
var offset = p.offset();

num = num + 1;        

if (num <=15) {

obj.id = obj.id + num;  <--- **changing the id (this one works fine but the other two dont**
obj1.id = obj1.id + num;   <--- changing the id
obj2.id = obj2.id + num;      <--- changing the id

document.body.appendChild(obj);
document.body.appendChild(obj1);
document.body.appendChild(obj2);

var left = offset.left + 275;

document.getElementById("chat").style.left = left + "px";

tell me if i am doing it wrong but this was the easiest way i thought off 告诉我我做错了什么,但这是我考虑的最简单方法

(ps i am a beginner at javascript) (ps我是javascript的初学者)

thanks to all that try to help... 感谢所有尝试提供帮助的人...

Edit 编辑

ok i clone this 好吧,我克隆这个

<div class="chat" id="chat">
<div id="ch" class="ch">
               <h2>Chat</h2></div>
               <div class="chatbox" id="chatbox">
               <div class="messages"></div>
               <textarea id="message" class="chatinp" 
               rows="3" cols="27"></textarea>
               <button class="send">Send</button></div>
</div>

and everytime it clones it changes the id of chat,ch and chatbox but keeping the original the same 每次克隆时,它都会更改chat,ch和chatbox的ID,但保持原始ID不变

like so... 像这样...

clone1 克隆1

<div class="chat" id="chat1">
<div id="ch1" class="ch">
               <h2>Chat</h2></div>
               <div class="chatbox" id="chatbox1">
               <div class="messages"></div>
               <textarea id="message" class="chatinp" 
               rows="3" cols="27"></textarea>
               <button class="send">Send</button></div>
</div>

Clone2 克隆2

<div class="chat" id="chat2">
<div id="ch2" class="ch">
               <h2>Chat</h2></div>
               <div class="chatbox" id="chatbox2">
               <div class="messages"></div>
               <textarea id="message" class="chatinp" 
               rows="3" cols="27"></textarea>
               <button class="send">Send</button></div>
</div>

Not sure, but if I'm right, you're trying to create a new 'chatnode'. 不确定,但是如果我是对的,那么您正在尝试创建一个新的“ chatnode”。 You'll have to traverse the childNodes array of the node you cloned to change id's. 您必须遍历克隆节点的childNodes数组才能更改id。 Try something like: 尝试类似:

function cloneChat(){
  var obj  = document.getElementById("chat").cloneNode(true),
      children = obj.childNodes
  ;
  num += 1;
  obj.id = obj.id+num;
  if (num<16){
      changeId(children,num);
  }
  //now appending obj to the document.body should be sufficient
  document.body.appendChild(obj);

  //change id recursively  
  function changeId(nodes, n){
   for (var i=0;i<nodes.length;i=i+1){
     if (nodes[i].childNodes){
           changeId(nodes[i].childNodes,n);
     }
     if(nodes[i].id && /^ch$|^chatbox$/i.test(nodes[i].id)) {
      nodes[i].id += String(n);
     }
   }
  }

}

See this jsfiddle for a working example 有关工作示例,请参见此jsfiddle

Furthermore, this code won't work: 此外,此代码将不起作用:

var p = $(".chat");
var offset = p.offset();

Because $(".chat") returns a list of nodes, where every node has it's own offset. 因为$(".chat")返回节点列表,所以每个节点都有自己的偏移量。

You seem to be using jQuery, so I suggest adding a 'jQuery' tag to your question. 您似乎正在使用jQuery,因此建议您在问题中添加“ jQuery”标签。 Maybe some jQuery whizzkid has a solution to offer. 也许有些jQuery whizzkid提供了解决方案。

In jQuery try to use 在jQuery中尝试使用

element.attr("id","newId");

See: http://api.jquery.com/attr/ 参见: http : //api.jquery.com/attr/

How about this function? 这个功能怎么样?

    function appendMe() {
        var elementsToClone = ["chat"];                       //Parent Elements to clone. This is the class name as well as the id
        var childrenToHandle = new Array();
        childrenToHandle["chat"] = ["ch"];          //Child elements mapping to keep sync. This is the class name as well as the id. Here we say that for the parent element chat, the inner elements to keep in sync is ch
        var itemCount = 0;
        for(i = 0; i < elementsToClone.length; i++) {
            var refObj = document.getElementById(elementsToClone[i]);
            if(refObj) {
                $("." + elementsToClone[i]).each(
                    function() {
                        if(this.id.match(/\d+$/g)) {
                            itemCount = this.id.match(/\d+$/g);
                        }
                    }
                );
                var newObj = refObj.cloneNode(true);
                newObj.id = elementsToClone[i] + ++itemCount;
                var childrenArray = childrenToHandle[elementsToClone[i]];
                if(childrenArray) {
                    $(childrenArray).each(
                        function() {
                            $(newObj).find("." + this).attr("id", this + itemCount);
                        }
                    );
                }
                document.body.appendChild(newObj);
            }
        }
    }

Since you're already using jQuery in your code, how about: 由于您已经在代码中使用了jQuery,因此:

var $obj = $("#chat").clone(),
    $obj1 = obj.find("#ch"),
    $obj2 = obj.find("#chatbox");

var p = $(".chat"),
    offset = p.offset();

num = num + 1;

if (num <= 15) {

    $obj.attr('id', $obj.attr('id') + num);
    $obj1.attr('id', $obj1.attr('id') + num);
    $obj2.attr('id', $obj2.attr('id') + num);

    $('body').append(obj);

    var newLeft = offset.left + 275;

    $('#chat').css({
        left: newLeft
    });

}

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

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