关于 jQuery 选择器的问题
最初,当用户单击删除按钮时,我希望删除整行元素。
这是为了在按下添加按钮时添加一行元素:
function btnPlsClickEvent(btn){
btn.remove(".buttonPlus");
$(".buttonMinus").show();
//$("#q" + questionCount).append("<p id='q'+ '" + ++questionCount + "' + ''>Question: " + questionCount + "<input type='text'>\
$("#q1").append("<p id='q'+ '" + ++questionCount + "' + ''>Question: " + questionCount + "<input type='text'>\
<button type='button' class='buttonPlus'></button>\
<button type='button' class='buttonMinus' id='"+ questionCount +"'></button></p>");
$(".buttonMinus:last").hide();
//Because it's dynamically appended, I re-bind them in this way:
//Re-binding
var newBtnPlus = $(".buttonPlus:last");
var newBtnMinus = $(".buttonMinus:last");
newBtnPlus.click(function(){
btnPlsClickEvent(newBtnPlus); //ATTENTION!
});
newBtnMinus.click(function () {
btnMnsClickEvent(this); //ATTENTION! Is it passing btn which is at the head of the function? If so, it should refer to Plus button.
});
alert("Add a question.");}
因此,如果我想删除整行,我应该找到它的父元素
并调用 .remove()。
function btnMnsClickEvent(btn) {
alert("You have deleted #" + btn.id + " question.");
btn.parent().remove();}
问题#1 是:我可以使用 btn 正确获取减号按钮的 id。但为什么我不能引用自身或其父级并将其删除呢?
问题 #2 是:为什么我可以在 btnPlsClickEvent() 的头部删除 btn 的类,但在 btnMnsClickEvent() 中却不能这样做?我知道我正在使用两种不同的方法将不同的对象传递给 btnPlsClickEvent() 和 btnMnsClickEvent()。他们有什么区别呢?
非常感谢, 阿什利
Initially, when users click the delete button, I want the whole line of elements are removed.
This is for adding a line of elements when add button is being pressed:
function btnPlsClickEvent(btn){
btn.remove(".buttonPlus");
$(".buttonMinus").show();
//$("#q" + questionCount).append("<p id='q'+ '" + ++questionCount + "' + ''>Question: " + questionCount + "<input type='text'>\
$("#q1").append("<p id='q'+ '" + ++questionCount + "' + ''>Question: " + questionCount + "<input type='text'>\
<button type='button' class='buttonPlus'></button>\
<button type='button' class='buttonMinus' id='"+ questionCount +"'></button></p>");
$(".buttonMinus:last").hide();
//Because it's dynamically appended, I re-bind them in this way:
//Re-binding
var newBtnPlus = $(".buttonPlus:last");
var newBtnMinus = $(".buttonMinus:last");
newBtnPlus.click(function(){
btnPlsClickEvent(newBtnPlus); //ATTENTION!
});
newBtnMinus.click(function () {
btnMnsClickEvent(this); //ATTENTION! Is it passing btn which is at the head of the function? If so, it should refer to Plus button.
});
alert("Add a question.");}
So, if I want to delete the whole line, I should find its parent
and call .remove().
function btnMnsClickEvent(btn) {
alert("You have deleted #" + btn.id + " question.");
btn.parent().remove();}
The question #1 is: I can correctly get the id of the minus button, using btn. But how come I can't refer to itself or its parent and remove it?
Q #2 is: Why I can remove btn's class at the head of btnPlsClickEvent() but I can't do so in btnMnsClickEvent()? I know I am using two different ways to pass different object to btnPlsClickEvent() and btnMnsClickEvent(). What's the differences of them?
Thank you very much,
Ashley
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1# 尝试将您的remove()行更改为:
函数获取html dom元素的对象btnMnsClickEvent(this);,并且您需要用jquery包装它
2#第二个函数获取jquery元素的对象,所以没有那里有问题
1# Try to change your remove() line to:
The function get object of the html dom element btnMnsClickEvent(this);, and you need to wrap it with jquery
2# the second function get object of jquery elements, so there is no problem there