简体   繁体   English

克隆div并更改值

[英]Clone div and change values

When pressing a button I want to make a new copy of the <div class="search-result"> and change the h3 inside it. 当按下按钮时,我想制作<div class="search-result">的新副本并更改其中的h3。

I tried to clone it but that would only clone the already consisting one and I don't know how to change the value of h3 in the new cloned one. 我试图克隆它,但是那只会克隆已经组成的那个,而我不知道如何在新克隆的那个中改变h3的值。 as in $(".search-result").clone().appendTo(".search"); $(".search-result").clone().appendTo(".search"); How do I change the value of the one I just cloned, since I have a loop to make more of these? 由于我有一个循环来制作更多的副本,因此如何更改刚克隆的副本的值?

Is there another method to do this? 还有另一种方法可以做到这一点吗?

<div class="col-xs-12 col-md-6 search" style="background-color: blue;">
                        <h2>Sök resultat</h2>
                        <div class="search-result">
                            <h3>Titel(year)</h3>
                            <input id="btnFavorite" type="submit" value="Favoritfilm" class="btn btn-warning">
                            <input id="btnArkiv" type="submit" value="Arkiv" class="btn btn-warning">
                        </div>

                    </div>

Try something like this. 尝试这样的事情。 In this example you first need to clone the node using cloneNode then append it to the parent div. 在此示例中,您首先需要使用cloneNode克隆节点,然后将其附加到父div。 Find the h3 tag inside the cloned div using find() and use html() to manipulate the inner html of h3 tag. 使用find()在克隆的div中find() h3标签,并使用html()处理h3标签的内部html。

            can = document.getElementById(".search_result")
            clone = can.cloneNode(".search_result");
            document.getElementById(".search").appendChild(clone);
            clone.find('h3').html( 'put new value here' );

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

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