jquery - 如何使用jquery通过id访问类?

标签 jquery class fieldset

我有一个 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/

相关文章:

javascript - 通过input type = file上传文件如何打开windows对话框窗口

JavaScript/jQuery : display key and value to JSON representation from input element

jquery - IE9不执行Ajax请求

C# 嵌套 - 外部类型的访问成员

c# - 是否可以在两个项目中声明一个部分类

css - 无法将字段集的内容居中

dependency-injection - 如何正确地将 ObjectManager 注入(inject)到 Fieldsets 中?

javascript - 我只想删除我单击的父标签

class - 为什么我可以将方法标记为隐式而不是构造函数?

html - 字段集中的 H-Scroll div