最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing a jQuery object in event delegation - Stack Overflow

programmeradmin3浏览0评论
// Snippet 1
$(document).on("keyup blur", "#selector_1_id", function() {
    // DO SOMETHING
});

$(document).on("keyup blur", "#selector_2_id", function() {
    // DO SOMETHING
});


// Snippet 2
var selector_1_id = $("input[name=selector_1_id]");
var selector_2_id = $("input[name=selector_2_id]");

$(document).on("keyup blur", selector_1_id, function() {
    // DO SOMETHING
});

$(document).on("keyup blur", selector_2_id, function() {
    // DO SOMETHING
});

Why do these snippets seem to be behaving differently? While the first one actually seems to work as ideal, that is keyup and blur being actually applied on the selector on keyup and blur event, while the other seems to be not working that ideally, it behaves like the snippet keeps on running always.

I am enabling and disabling input types on the live website using JavaScript.

// Snippet 1
$(document).on("keyup blur", "#selector_1_id", function() {
    // DO SOMETHING
});

$(document).on("keyup blur", "#selector_2_id", function() {
    // DO SOMETHING
});


// Snippet 2
var selector_1_id = $("input[name=selector_1_id]");
var selector_2_id = $("input[name=selector_2_id]");

$(document).on("keyup blur", selector_1_id, function() {
    // DO SOMETHING
});

$(document).on("keyup blur", selector_2_id, function() {
    // DO SOMETHING
});

Why do these snippets seem to be behaving differently? While the first one actually seems to work as ideal, that is keyup and blur being actually applied on the selector on keyup and blur event, while the other seems to be not working that ideally, it behaves like the snippet keeps on running always.

I am enabling and disabling input types on the live website using JavaScript.

Share Improve this question edited Jul 25, 2013 at 21:10 iConnor 20.2k14 gold badges66 silver badges97 bronze badges asked Jul 25, 2013 at 20:43 J DJ D 1,8182 gold badges20 silver badges20 bronze badges 5
  • 3 Check docs: "[selector] A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element." – elclanrs Commented Jul 25, 2013 at 20:45
  • $("input[name=selector_1_id]") is NOT a selector! – Kevin B Commented Jul 25, 2013 at 20:57
  • @KevinB yes it is, where did that e from. – iConnor Commented Jul 25, 2013 at 21:02
  • @KevinB jsfiddle/cYPkE – iConnor Commented Jul 25, 2013 at 21:02
  • 1 It came from the question. It's not a selector, it's a jQuery object containing an element. A selector in this case must be a string, as noted in the documentation for .on. – Kevin B Commented Jul 25, 2013 at 21:23
Add a ment  | 

3 Answers 3

Reset to default 10

You cannot pass a element through that function like that, it has to be the string selector.

$(document).on("keyup blur", selector_2_id, function() {
    // DO SOMETHING
});

Won't work, in-fact the keyup and blur will be triggered every single time on the document rather than the selector

If you have the element in a variable there is no need to do this because you have a reference to the element anyway you only use this if the element can be added any time and you don't have a reference to it.

So

selector_2_id.on('keyup blur', function() {
   // DO SOMETHING
});

Should work perfectly.

Deeper look

A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element.

So what this means is this

$(document).on('keyup blur', selector_2_id, function(){
  ^------^ = 1               ^-----------^ = 2
});

If 2 is null or omitted then 1 will be used for the event

As per the api , the selector need's to be a string and not a jquery object

selector Type: String - A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element.

http://api.jquery./on/

I believe this is what you are trying to do:

// Snippet 2
var selector_1_id = "input[name=selector_1_id]";
var selector_2_id = "input[name=selector_2_id]";

$(document).on("keyup blur", selector_1_id, function() {
    // DO SOMETHING
});

$(document).on("keyup blur", selector_2_id, function() {
    // DO SOMETHING
});

The above will work (passing a string var as the selector), but why not just:

$(document).on("keyup blur", "input[name=selector_1_id]", function() {
    // DO SOMETHING
});

$(document).on("keyup blur", "input[name=selector_2_id]", function() {
    // DO SOMETHING
});

...unless you have a greater context in which you create this string or do something with it.

发布评论

评论列表(0)

  1. 暂无评论