Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jqueryclone创建div元素,如何设置ID标记?_Jquery_String_Loops - Fatal编程技术网

使用jqueryclone创建div元素,如何设置ID标记?

使用jqueryclone创建div元素,如何设置ID标记?,jquery,string,loops,Jquery,String,Loops,我使用以下jQuery代码块将一些html从一个地方复制到另一个地方: var newLine = $('#popup-clone .popup-contents').last().clone(); newLine.find('.popup-title').html("hello world"); $('#popup-container').append(newLine); 这会在循环中运行几次,因此html的呈现方式如下: <div id="popup-container"

我使用以下jQuery代码块将一些html从一个地方复制到另一个地方:

var newLine  = $('#popup-clone .popup-contents').last().clone();
newLine.find('.popup-title').html("hello world");

$('#popup-container').append(newLine);
这会在循环中运行几次,因此html的呈现方式如下:

    <div id="popup-container" style="display: block;">
        <div class="popup-contents">
            <a class="close">close this popup</a>
            <span class="popup-title">Title 1</span>
            <span class="popup-description"></span>
            <span class="popup-type"></span>
            <span class="popup-open"></span>
        </div>
        <div class="popup-contents">
            <a class="close">close this popup</a>
            <span class="popup-title">Title 2</span>
            <span class="popup-description"></span>
            <span class="popup-type"></span>
            <span class="popup-open"></span>
        </div>
    </div>

关闭此弹出窗口
标题1
关闭此弹出窗口
标题2
等等

我想用class=“popup contents”为每个div添加一个唯一的ID,但我不知道如何才能做到这一点?感谢您的帮助。

使用“attr”:

使用“attr”:


如果id可以是唯一的随机id,那么我在过去所做的就是使用UUID/GUID生成器从生成随机id

那么这只是一个行动的问题

newLine.attr( "id", guid );

如果id可以是唯一的随机id,那么我在过去所做的就是使用UUID/GUID生成器从生成随机id

那么这只是一个行动的问题

newLine.attr( "id", guid );

使用
prop
代替
attr


使用
prop
代替
attr


通常可以避免为克隆的元素提供id

元素通常需要id的原因是(a)作为CSS样式的参考,或者(b)可以通过javascript/jQuery来解决


在这两种情况下,您都可以在创建克隆元素时对其进行寻址

通常可以避免为克隆的元素提供id

元素通常需要id的原因是(a)作为CSS样式的参考,或者(b)可以通过javascript/jQuery来解决


在这两种情况下,您都可以在创建克隆元素时对其进行寻址

谢谢,但是在我的特定实例中,我希望基于DataSethanks分配特定ID,而在我的特定实例中,我希望基于dataset分配特定ID
newLine.prop('id', 'prefix' + someindex)