jQuery 图片上传功能代码优化:移除元素的最佳位置
The code you provided appears to be written in JavaScript and jQuery. Here is a breakdown of the code:
\
- The code is using the jQuery
$(document).on('click', '.qmulimg', function() { ... })event handler to handle a click event on elements with the class "qmulimg".\ - Inside the event handler,
var inputp = $(this);is used to assign the clicked element to the variable "inputp".\ $(this).remove();is used to remove the clicked element from the DOM. \- The code then initializes an empty array called "imgid".\
- It finds elements with the class "qq_muti_img" within the parent element with the class "qq_u_img" and iterates over them using the
.each()method. \ - Inside the loop, the data attribute "data_id" of each element is retrieved using
$(this).attr('data_id')and pushed into the "imgid" array. \ - The "imgid" array is converted to a comma-separated string using the
toString()method and assigned to the "imgidlist" variable. \ - The value of the "imgidlist" variable is then assigned to the value of an input element with the class "q_uid_img" that is found within the parent element with the class "qq_uploader". \
- Finally,
$(this).remove();is called again to remove the clicked element from the DOM.
Regarding your solution, it seems like you have realized that$(this).remove();should be placed at the end of the event handler because it is removing the main element that the event is being triggered on. This ensures that all the necessary actions are performed before removing the element.
If you have any further questions or concerns, please let me know.
原文地址: https://www.cveoy.top/t/topic/qlhv 著作权归作者所有。请勿转载和采集!