jQuery优先级执行 [英] jquery priority execution
问题描述
有人可以帮我吗?
$('#n').click(function() {
$(this).parent().append('<a href="javascript:void()"> delete</a>');
$(this).next().click(function() {
alert('clicked'); //this not working
});
$(this).blur(function() {
$(this).next().remove();
});
});
JS小提琴演示;问题是blur()
事件在click()
事件之前执行.
JS Fiddle demo; the problem is that the blur()
event is executed before click()
event.
推荐答案
您可以使用超时将删除操作延迟几毫秒.
You can use a timeout to postpone the removal for some milliseconds.
示例: http://jsfiddle.net/vkun9/7/
$(this).blur(function() {
var _this = this;
setTimeout(function(){$(_this).next().remove();},100);
});
我还将blur
附件移到了点击处理程序的外部,因为每次单击元素时它都会添加一个附加的内容,并且将点击处理程序更改为焦点,以避免重复出现多个remove
按钮单击输入,如@dererosaur所述.
I also moved the blur
attaching to be outside of the click handler, as it was adding an additional one each time the element was clicked, and changed the click handler to the focus to avoid multiple remove
buttons from repeated clicking on the input, as @dheerosaur noted.
如此
$('#n')
.focus(function() {
$(this).parent().append('<a href="javascript:void()"> delete</a>');
$(this).next().click(function() {
alert('clicked'); //this not working
});
})
.blur(function() {
var _this = this;
setTimeout(function(){$(_this).next().remove();},100);
});
不过,您遇到的问题不是问题.这是正常的行为,因为该元素需要失去焦点(触发模糊),然后另一个元素才能拥有焦点.
What you experience, though, is not a problem. It is the normal behaviour, as the element need to lose focus (fires the blur) before another element can have it.
您还应该将标签for
属性与输入元素的ID相匹配.
You should also match the label for
attribute with the id of the input element.
这篇关于jQuery优先级执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!