Home > Web Front-end > JS Tutorial > jquery (javascript) automatic serial number and attribute number implementation code_jquery

jquery (javascript) automatic serial number and attribute number implementation code_jquery

WBOY
Release: 2016-05-16 17:52:01
Original
1582 people have browsed it

Automatic sequence numbering and automatic attribute numbering, the effect diagram is as follows:

Implementation principle:
Addition and deletion are reverse processes, and the implementation is consistent.
When adding, add the element append method to the parent container, set all custom attribute numbers and sequence numbers to empty, and then reassign custom attribute numbers and sequence numbers through the $.each method.

Copy code The code is as follows:

$.each(items, function (k, v) {
$(this).attr("opt", "mopt" k);
serials.eq(k).html(k);
});

When deleting, bind the event live method to all delete buttons, delete the element from the parent container with the detach method, and set all custom attribute numbers and sequence numbers to empty, and then re-customize them through the $.each method. Attribute number and sequence number assignment.
Copy code The code is as follows:

$("#test .del").live( "click", function () { //Bind click events for delete buttons
var dels = test.find(".del"); //All deleted buttons
var delnum = dels.index($ (this)); //The index value of the current delete button
var items = test.find(".item");
items.eq(delnum).detach(); //Detach it from the parent container Delete this node
items.attr("opt", "");
var serials = test.find(".serial");
serials.html("");
$. each(items, function (k, v) { //Reassign custom attributes with numbers
$(this).attr("opt", "mopt" k);
serials.eq(k). html(k);
});
});

Examples are as follows:

[Ctrl A select all Note:
If you need to introduce external Js, you need to refresh to execute ]
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template