jquery动态id选择器,删除然后添加类
我有一个表,其中所有行都有一个 div 单元格,该单元格具有动态生成的 id,格式为 btn-insertidhere。
当选择表格行时,我想选择这个div id,然后删除一个类并将其更改为另一个类。这取决于我想要将按钮图像从添加符号更改为单击时的删除符号。
javascript 代码:
$('*[class^=day] tbody tr[id^=band]').live('click', function() {
var DivId = $(this).find('div.add').attr('id');
alert(DivId);
$('DivId').removeClass('add').addClass('del');
$('table#fri_myTimes tbody').append($(this)).fadeIn(1000);
return false;
});
这是动态生成的代码的 html 片段:
<tr id="band-Modest-Mouse">
<td>Modest Mouse</td>
<td>15:25:00</td>
<td>16:10:00</td>
<td>45</td>
<td><div id="btn-Modest-Mouse" class="add"> </div></td>
</tr>
如您所见,我想将“添加”类更改为删除“类”。表格上的所有表格行都是这样生成的,所以如您所见,我采用了通配符方法,这似乎有效,因为显示的警报显示了正确的 div id。我只需要换个班级就可以了!
谢谢!
I have a table, with rows which all have a cell of a div with a dynamically generated id in the format of btn-insertidhere.
When the table row is selected, I want to select this div id, then remove a class and change it to another one. This is down to me wanting to have a button image change from an add symbol to a delete symbol which when clicked.
javascript code:
$('*[class^=day] tbody tr[id^=band]').live('click', function() {
var DivId = $(this).find('div.add').attr('id');
alert(DivId);
$('DivId').removeClass('add').addClass('del');
$('table#fri_myTimes tbody').append($(this)).fadeIn(1000);
return false;
});
This is an html snippet of the dynamically generated code:
<tr id="band-Modest-Mouse">
<td>Modest Mouse</td>
<td>15:25:00</td>
<td>16:10:00</td>
<td>45</td>
<td><div id="btn-Modest-Mouse" class="add"> </div></td>
</tr>
As you can see i want to change the 'add' class to a delete 'class'. All the table rows on the table is generated like this, so as you can see i've gone for the wildcard approach, which seems to work because the alert shown, shows the correct div id. I just need to change the class!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须通过删除 '.attr(id)' 部分来获取 div 的 jq 对象
,或者在 divId 选择器中添加 #
You either have to get the jq object for the div by removing the '.attr(id)' part
or add a # in the divId selector
尝试删除 DivId 周围的引号。它是一个变量名称,不应包含在引号中。像这样:
Try removing the quotes around DivId. It's a variable name and shouldn't be in quotes. As such:
由于您的变量 DivId 是一个 jquery 对象,因此您根本不需要
$()
:Since your variable DivId is a jquery object, you don't need the
$()
at all: