我有一个 html 如下:
<fieldset id="question1">
<legend class='legend'>...</legend>
...
<input type="text" name="label_no1" id="label_no1" autocomplete="off">
</fieldset>
在java脚本中,我正在克隆字段集,但我想访问它的元素来更改id和一些文本。
我尝试了这个,但没有成功:
$('#question1").siblings(".legend").html="any text"; \\ I also tried children
我还希望能够访问字段集中的所有输入,以便更改它们的 ID。 有什么帮助吗?
最佳答案
试试这个:
$clone = $('#question1').clone();
$clone.attr('id','some_new_id');
// you don't need to search by class or id you can search for tags also,
// so you can get rid of some redundancy and write <legend>…</legend>
// instead of <legen class="legend">…</legend>
// siblings won't work with your example, because legend is a child not a sibling
// html= won't work either html on a jQuery object is a method not an attribute
$clone.find('legend').html("New text for the Legend");
关于jquery - 如何使用jquery通过id访问类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3109880/