代码中this指向疑问及jQuery选择器传参原因咨询
Hey Jerry, let's unpack your two technical questions using your code as reference:
疑问1:代码中的this指向什么?
In the .edit click handler, this refers to the specific DOM element that triggered the click event — which is the individual .edit button you just clicked.
I get why you might have thought it points to the .edit class as a whole, but jQuery binds the event to each matching element separately. So when you click the edit button on your first blog entry, this is that exact <input type="button" class="edit"> element, not all elements with the .edit class. That's why using $(this).closest(".blogContainer") works: it starts from the clicked button and climbs up the DOM tree to find its nearest parent .blogContainer.
疑问2:为什么要把container传入jQuery选择器来获取输入框的值?
Great question — this is all about scoping your selection to the right part of the DOM.
Your code generates multiple .blogContainer elements, each with its own .titleInput, .contentInput, and .dateInput. If you just used $(".titleInput").val(), jQuery would select all .titleInput elements on the page and return the value of the first one — which is not what you want (you need the input values from the same blog entry as the clicked edit button).
By passing container as the second argument to the jQuery selector ($(".titleInput", container)), you're telling jQuery: "Only look for .titleInput elements inside this specific container element". This is shorthand for container.find(".titleInput"), and it ensures you're grabbing the input values from the exact blog entry you're editing, not any other entries on the page.
Quick extra tip
One thing to note: your for loop uses var for blogID, which has function-level scope. If you ever run into issues where the wrong blogID is being picked up, switching to let blogID = i (block-level scope) would fix that, since each iteration of the loop would have its own blogID variable.
内容的提问来源于stack exchange,提问作者Jerry

