使用 jQuery 动态地将 1 添加到附加 div 上的 id/class
我试图将 1 添加到任何新附加的 id/class:
jQuery:
$(function(){
$("#add-new-student").click(function() {
$("ul#student-list").append('<li class="student3"/>Student</li>');
$("div#student-courses").append('<div id="student3" class="student-information">'
+ '<div id="student3-enrolled-courses">'
+ '<span class="english">English</span>'
+ '<span class="technology">Technology</span>'
+ '</div>'
+ '</div>'
);
});
});
HTML:
<input type="submit" id="add-new-student" value="Add New Student">
<ul id="student-list">
<li class="student1"/>Student</li>
<li class="student2"/>Student</li>
</ul>
<div id="student-courses">
<div id="student1" class="student-information">
<div id="student1-enrolled-courses">
<span class="english">English</span>
<span class="technology">Technology</span>
</div>
</div>
<div id="student2" class="student-information">
<div id="student2-enrolled-courses">
<span class="english">English</span>
<span class="technology">Technology</span>
</div>
</div>
</div>
我设置了两个小提琴:
http://jsfiddle.net/KHfyY/ 使用 after 而不是追加并查找 div.student-information:最后
http://jsfiddle.net/KHfyY/1/ 使用此处的代码并附加两者中的
任意一个最适合这个的应该是我们可以使用的。
如果没有学生,那么第一个班级/ID 应该是student1。如果代码中有学生,则应在最后一个学号的基础上加1。知道如何做到这一点吗?
I am trying to add 1 to any newly appended id/class:
jQuery:
$(function(){
$("#add-new-student").click(function() {
$("ul#student-list").append('<li class="student3"/>Student</li>');
$("div#student-courses").append('<div id="student3" class="student-information">'
+ '<div id="student3-enrolled-courses">'
+ '<span class="english">English</span>'
+ '<span class="technology">Technology</span>'
+ '</div>'
+ '</div>'
);
});
});
HTML:
<input type="submit" id="add-new-student" value="Add New Student">
<ul id="student-list">
<li class="student1"/>Student</li>
<li class="student2"/>Student</li>
</ul>
<div id="student-courses">
<div id="student1" class="student-information">
<div id="student1-enrolled-courses">
<span class="english">English</span>
<span class="technology">Technology</span>
</div>
</div>
<div id="student2" class="student-information">
<div id="student2-enrolled-courses">
<span class="english">English</span>
<span class="technology">Technology</span>
</div>
</div>
</div>
I set up two fiddles:
http://jsfiddle.net/KHfyY/ uses after instead of append and finds the div.student-information:last
http://jsfiddle.net/KHfyY/1/ uses the code here with append for both
Whichever one is best for this should be the one we can use.
If there are no students, then the first class/id's should be student1. If there are students as in the code, it should increment it by 1 based on the last student number. Any idea how this can be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
工作示例@:http://jsfiddle.net/KHfyY/5/
Try this:
Working example @: http://jsfiddle.net/KHfyY/5/
更新了
http://jsfiddle.net/KHfyY/7/
像这样吗?
只需根据一开始有多少学生提前设置变量
n
即可。在此示例中,n 是通过对列表中已存在的学生进行计数来自动计算的。
Updated
http://jsfiddle.net/KHfyY/7/
Like this?
Just set the variable
n
in advance based on how many students you have at the beginning.In this example, the n is calculated automatically by counting the student already present in the list.