使用 jQuery 动态地将 1 添加到附加 div 上的 id/class

发布于 2024-11-19 20:35:44 字数 1618 浏览 2 评论 0原文

我试图将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你在我安 2024-11-26 20:35:44

试试这个:

$(function(){
    $("#add-new-student").click(function() {
        var studentList = $("ul#student-list");
        var currentCount = $("li", studentList).length + 1;
    studentList.append('<li class="student' + currentCount +'">Student</li>');
    $("div#student-courses").append('<div id="student' + currentCount +'" class="student-information">'
    + '<div id="student' + currentCount +'-enrolled-courses">'
    + '<span class="english">English</span>'
    + '<span class="technology">Technology</span>'
    + '</div>'
    + '</div>'
    );
    });
});

工作示例@:http://jsfiddle.net/KHfyY/5/

Try this:

$(function(){
    $("#add-new-student").click(function() {
        var studentList = $("ul#student-list");
        var currentCount = $("li", studentList).length + 1;
    studentList.append('<li class="student' + currentCount +'">Student</li>');
    $("div#student-courses").append('<div id="student' + currentCount +'" class="student-information">'
    + '<div id="student' + currentCount +'-enrolled-courses">'
    + '<span class="english">English</span>'
    + '<span class="technology">Technology</span>'
    + '</div>'
    + '</div>'
    );
    });
});

Working example @: http://jsfiddle.net/KHfyY/5/

小姐丶请自重 2024-11-26 20:35:44

更新了

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文