关于 jQuery 选择器的问题

发布于 2024-12-09 01:08:23 字数 1508 浏览 0 评论 0原文

最初,当用户单击删除按钮时,我希望删除整行元素。

这是为了在按下添加按钮时添加一行元素:

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 技术交流群。

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

发布评论

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

评论(1

陌生 2024-12-16 01:08:23

1# 尝试将您的remove()行更改为:

$( btn ).parent().remove();

函数获取html dom元素的对象btnMnsClickEvent(this);,并且您需要用jquery包装它

2#第二个函数获取jquery元素的对象,所以没有那里有问题

1# Try to change your remove() line to:

$( btn ).parent().remove();

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

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