そのため、いくつかのHTMLを複製して、ドキュメントに追加したいと思います。これは典型的なHTMLフォームのものです。しかし、<label for="something">
要素IDに基づいて機能するようなものなので、jquery'd要素内の各HTML要素がclone()
、複製されたすべての対応物とは別に、一意のIDを持つことができれば便利です。これをどのように行うことができるかを推測するのと同じように、最初の要素内のIDにすべて一意の文字列を含めることができるかどうか疑問に思います。次に、どういうわけか要素をトラバースして、その文字列を_1、_2、_3などに置き換えることができます。
あまり進んでいませんが、こんな感じでうまくいくようです。
$(document).ready(function(){
var toReplace = "containerUniqueCharacterString1234";
var copy = $('#containerUniqueCharacterString1234').clone(true);
copy[0].style.display = "block"; //so i can reference the first element like this
console.log(copy[0]); //[object HTMLDivElement]
$('body').append(copy);
$.each(copy, function(index, value){
var children = copy[index].childNodes;
console.log(children); //[object NodeList]
var len = children.length;
if (copy[index].childNodes.length > 0){
if (copy[index].childNodes.hasOwnProperty('id')){
//replace the toReplace string with an incremented number here(?)
//annnnd this is where i've gotten in too deep and made this overly complex
}
}
$('#otherResults').append(len);
});
});
http://jsbin.com/ecojub/1/edit
または、これを行うためのはるかに簡単な方法があるかもしれません。ありがとう!