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

javascript - Selector only working after inspectingselecting element - Stack Overflow

programmeradmin0浏览0评论

I have this code here:

$(document).ready(function() {
    debugger;
    $("div[id^='stage_']").click(function (e) { alert('Hello'); });
});

The weird thing I can't explain is, that when I execute the selector once I'm in the console when reaching the debugger statement, it returns an empty array, []

But when I step out and go on the page, then hit Ctrl-Shift-C in Chrome to start inspecting and click on some of the div's that have the ID I'm looking for then execute the selector again in the console, now I have the elements I'm expecting.

I have even tried this here so to validate whether it was an async. loading issue (this is a system over which I don't have all the control). but still, when reaching the debugger, the selector doesn't work - even after waiting 10 seconds (which then I'm pretty sure the div's are there). I still have to go in inspector so jQuery recognize the elements.

$(document).ready(function() {
    //debugger;
    setTimeout(function() {
        debugger;
        $("div[id^='stage_']").click(function (e) { alert('allo'); });
    }, 10000);
});

Why would jQuery only be aware of elements that I've clicked on with Chrome's inspector ?

I have this code here:

$(document).ready(function() {
    debugger;
    $("div[id^='stage_']").click(function (e) { alert('Hello'); });
});

The weird thing I can't explain is, that when I execute the selector once I'm in the console when reaching the debugger statement, it returns an empty array, []

But when I step out and go on the page, then hit Ctrl-Shift-C in Chrome to start inspecting and click on some of the div's that have the ID I'm looking for then execute the selector again in the console, now I have the elements I'm expecting.

I have even tried this here so to validate whether it was an async. loading issue (this is a system over which I don't have all the control). but still, when reaching the debugger, the selector doesn't work - even after waiting 10 seconds (which then I'm pretty sure the div's are there). I still have to go in inspector so jQuery recognize the elements.

$(document).ready(function() {
    //debugger;
    setTimeout(function() {
        debugger;
        $("div[id^='stage_']").click(function (e) { alert('allo'); });
    }, 10000);
});

Why would jQuery only be aware of elements that I've clicked on with Chrome's inspector ?

Share Improve this question edited Jul 13, 2016 at 17:14 Francis Ducharme asked Jul 13, 2016 at 16:59 Francis DucharmeFrancis Ducharme 4,9877 gold badges46 silver badges86 bronze badges 7
  • Is this/these element(s) added in the DOM following any async call (ajax e.g)? If so, delegate click event to any static container – A. Wolff Commented Jul 13, 2016 at 17:04
  • @A.Wolff Actually, this from a system that I don't have full control over. If that is the case, how can I tell jQuery to only execute my code when everything is loaded? I thought .ready would do the job. – Francis Ducharme Commented Jul 13, 2016 at 17:07
  • No, jq ready pseudo event is when the DOM is ready, HTML parsed. It is different than window load event. So firstly try using as wrapper $(window).on('load', function(){...});. If still not working just delegate it using $(document).on('click',"div[id^='stage_']", function(e){ alert('Hello'); }); (no needs of any event wrapper if event is delegated to document level). If delegation doesn't work, then something else stop your click event propagation. Other possibility would be to capture click event (jq doesn't support it) and filter the event target inside click handler – A. Wolff Commented Jul 13, 2016 at 17:09
  • @A.Wolff I've added some details to my question – Francis Ducharme Commented Jul 13, 2016 at 17:15
  • 1 Well, are you sure this content isn't loaded inside an iframe? – A. Wolff Commented Jul 13, 2016 at 17:18
 |  Show 2 more comments

2 Answers 2

Reset to default 33

I know it's a bit late but when you open Dev Tools in Chrome the execution context is set to top. If your controls are located within an iFrame, that is a different context, not accessible from top. Use the dropdown to select your iFrame's context and your jQuery will return an element.

The reason it works when you inspect an element, is Chrome has selected the execution context for you already.

Discussion about iFrame context in Dev Tools

Using the "on", it works even if the element exists after the page loads.

$(document).ready(function(){ 
  //$("div[id^='stage_']").click( function (e) { alert('Hello'); });
  $("body").on('click','div[id^="stage_"]', function (e) { alert('Hello'); });
  $('body').html('<div id="stage_1">teste1</div>' +
                  '<div id="stage_2">teste2</div>' +
                  '<div>blabla</div>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

doc: http://api.jquery.com/on/

发布评论

评论列表(0)

  1. 暂无评论