gpt4 book ai didi

javascript - 在 HTMLString 中找到的元素中设置 attr(name)

转载 作者:行者123 更新时间:2023-12-02 15:44:55 32 4
gpt4 key购买 nike

我使用Jquery保存在html_data变量HTML代码中。特别是

Jquery(document).ready(function(){

html_data = $('.row').html();

)};



<div class = "row">
<div class = "col-md-12">
<select multiple="multiple" id ="sel1">
<option value="1"> Element1 </opion>
<option value="2"> Element2 </opion>
<option value="3"> Element3 </opion>
</select>
</div>
</div>

<div class ="before"></div>

然后我在 Jquery 中有一个我编写的函数

function newRow(){
new_html = html_data;
$(new_html).find('select').attr('name', 'myNewSelect');
$(new_html).insertBefore('.before');
}

已创建具有选择多个的新,但未设置属性名称。我该如何做到这一点?

最佳答案

您不需要获取.row元素的html()。您可以使用jquery的.clone()方法,然后使用相同的.insertbefore()方法将其附加在.before之前

function newRow(){
var new_html = $('.row .col-md-12').clone(true);
new_html.insertBefore('.before');
new_html.find('select').attr('name', 'myNewSelect');
}

这是 fiddle :https://jsfiddle.net/eL43e2vs/

关于javascript - 在 HTMLString 中找到的元素中设置 attr(name),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32285422/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com