Javascript 输入按钮onClick删除最近的div
呃,为什么这不起作用,我在我的网站上多次使用类似的代码。但是现在不起作用了 HTML 看起来$(这个)不是我的按钮。我使用alert($(this.val())进行了测试,但没有使用hependJavascript 输入按钮onClick删除最近的div,javascript,jquery,Javascript,Jquery,呃,为什么这不起作用,我在我的网站上多次使用类似的代码。但是现在不起作用了 HTML 看起来$(这个)不是我的按钮。我使用alert($(this.val())进行了测试,但没有使用hepend 将类添加到button元素并将处理程序绑定到button如何 <div> <span>a</span> <input type='hidden' /> <input type='button' class'removeDiv' /
将类添加到button元素并将处理程序绑定到button如何
<div>
<span>a</span>
<input type='hidden' />
<input type='button' class'removeDiv' />
我的div也是从clientside和serverside创建的。添加onclick函数,而不是在新项目上解除所有绑定,这非常容易,不是吗 在这种情况下,您可以使用委派事件。见下文
$(function () {
//Replace document with any closest container that is available on load.
$(document).on('click', '.removeDiv', function () {
$(this).parent().remove();
// As I see the input is direct child of the div
});
});
将类添加到button元素并将处理程序绑定到button如何
<div>
<span>a</span>
<input type='hidden' />
<input type='button' class'removeDiv' />
我的div也是从clientside和serverside创建的。添加onclick函数,而不是在新项目上解除所有绑定,这非常容易,不是吗 在这种情况下,您可以使用委派事件。见下文
$(function () {
//Replace document with any closest container that is available on load.
$(document).on('click', '.removeDiv', function () {
$(this).parent().remove();
// As I see the input is direct child of the div
});
});
这应该管用
HTML
这应该管用
HTML
既然已经在使用jQuery,为什么不将事件处理程序与jQuery绑定呢?我的div也是从clientside和serverside创建的。添加onclick函数很容易,而不是在新项上取消绑定或重新绑定所有项,不是吗?@NovkovskiStevoBato如果您担心委托事件会被动态创建,那么可以使用委托事件。既然您已经在使用jQuery,为什么不将事件处理程序与jQuery绑定?我的div也是从clientside和serverside创建的。添加onclick函数很容易,而不是在新项目上取消绑定重新绑定,不是吗?@NovkovskiStevoBato如果您担心委托事件被动态创建,您可以使用委托事件。您的编辑代码对我来说很好。我不知道我能分配什么样的点击功能,比如:)你的编辑代码对我来说很好。我不知道我能分配什么样的点击功能,比如:)
$(function () {
//Replace document with any closest container that is available on load.
$(document).on('click', '.removeDiv', function () {
$(this).parent().remove();
// As I see the input is direct child of the div
});
});
<div><span>a</span><input type='hidden' /> <input type='button' onClick='Remove(this)' />
function Remove(obj){
$(obj).closest('div').remove();
}