javascript - Why $('div#my') is returning Object Reference instead of HTML Tag? -
this question has answer here:
i don't know happen chrome browser, of sudden behavior of doing $('div#my') in console totally different before. 1 time i've experienced later somehow recovered, don't know how reproduce it, , today happened again.
please watch video:http://peaceevertvimg.org/jq.php.
in video $('div#my') in 2 different browsers:
the first browser not chrome believe imitates chrome behavior expect , have been experienced. because chrome not working expected have use demonstrate expection: when $('div#my)` see directly html tag, , can see tag's html content, "something" in case.
in contrast, in chrome browser, result different, when $('div#my') see object(n.fn.init), , can't see "something" immediately, of course inconvenient. before, pretty sure not this, behavior in first browser.
the simple webpage in video http://peaceevertvimg.org/jquery.php, can go test in chrome browser. , pretty sure of see first behavior. happened chrome?(i've disabled expansions , updated latest version)
by way, "html tag" , "object reference" right words describe these 2 different outcome?
*************update*************
if video not sufficient understand ask , want fix, these 2 pictures may help.
picture#1: "normal" behavior i've expected: 
picture#2: current behavior experiencing: 
you can see big differences, first 1 more intuitive, revealing key information immediately, while 2nd 1 not, @ least me. causes problem , how go first one?
$('div#my') doesn't return dom reference. returns jquery wrapper around found elements.
$('div#my')[0] return dom reference. or, forget jquery , use:
document.getelementbyid("my"); ...and dom reference directly
also, since there should/will ever 1 element given id, unnecessary use div#my, use #my.
assuming have <div id=somediv>, , write:
console.log($("#somediv")); console.log($("#somediv")[0]); chrome shows this:
in first log, see result jquery object contains 1 element (the div). in second, see element directly.
now, depending on version of chrome have, may see first 1 reported [object object], doesn't change underlying result.

Comments
Post a Comment