我有以下。有什么方法可以将这两个事件结合起来吗?我需要某种 foreach 功能吗?
$('#AccountID').focus(function() {
$('option[value="99"]', this).remove();
});
$('#TopicID').focus(function() {
$('option[value="99"]', this).remove();
});
我有以下。有什么方法可以将这两个事件结合起来吗?我需要某种 foreach 功能吗?
$('#AccountID').focus(function() {
$('option[value="99"]', this).remove();
});
$('#TopicID').focus(function() {
$('option[value="99"]', this).remove();
});
你只需要一个不同的选择器:
$('#AccountID, #TopicID').focus(...);
或者,更好的是,添加一个公共类并改用它。
这取决于组合事件的含义。你可以这样做:
$('#AccountID, #TopicID')
.focus(function () {
$('option[value="99"]', this).remove();
});
jQuery 选择器使用 css 选择器,所以选择器的a,b
意思是,选择a
和 b
$('#AccountID, #TopicID').focus(function () {
$('option[value="99"]', this).remove();
});
请注意,当您为 jQuery 函数提供上下文时,它会以这种方式执行:
$(this).find('option[value="99"]').remove();
所以你可以自己做,节省(虽然很小)的旅行和开销。
...
...
...
// HANDLE: $(expr, context)
// (which is just equivalent to: $(context).find(expr)
} else {
return this.constructor( context ).find( selector );
}