jquery - confused by the javascript console log output of function variables - Stack Overflow

I am a beginner when it es to javascriptjquery..so if this question sounds silly,please forgive me.Whi

I am a beginner when it es to javascript/jquery..so if this question sounds silly,please forgive me.

While going through this tutorial ,I tried to write to javascript console ,values of some of the variables in the functions-so that I can better understand how the function works

$(document).ready(function(){
    $("div.post h2 a").click(function () {
    var a    = $(this),
    href = a.attr('href'),
    content  = a.parent().next();
    console.log('a='+a);
    console.log('a.get(0)='+a.get(0));
    console.log('a parent='+a.parent());
    console.log('a parent.get(0)='+a.parent().get(0));
    console.log("href="+href);
    console.log('content='+content);
    content.load(href + " #content");
    return false; 
});

I have modified the html slightly

<div class="post">
    <h2 id="h21"><a href="other/mypage.html">My Page</a></h2>
    <div class="content">
        Teaser text1...
    </div>
</div>
<div class="post">
    <h2 id="h22"><a href="other/myotherpage.html">My Other Page</a></h2>
    <div class="content">
        Teaser text2...
    </div>
</div>
});

when I click on the first link,I get this console log output

a=[object Object]
a.get(0)=file:///home/me/dev/misc/other/mypage.html
a parent=[object Object]
a parent.get(0)=[object HTMLHeadingElement]
href=other/mypage.html
content=[object Object]

I thought the $(this) expression in the function would be the element which was clicked(ie the first <a element).Why does it appear as [object Object].I couldn't make out how a.get(0) bees file:///home/me/dev/misc/other/mypage.html

Similarly,shouldn't the variable content be equal to the first div (containing Teaser text1)? Why does it appear as [object Object]?

I am a beginner when it es to javascript/jquery..so if this question sounds silly,please forgive me.

While going through this tutorial ,I tried to write to javascript console ,values of some of the variables in the functions-so that I can better understand how the function works

$(document).ready(function(){
    $("div.post h2 a").click(function () {
    var a    = $(this),
    href = a.attr('href'),
    content  = a.parent().next();
    console.log('a='+a);
    console.log('a.get(0)='+a.get(0));
    console.log('a parent='+a.parent());
    console.log('a parent.get(0)='+a.parent().get(0));
    console.log("href="+href);
    console.log('content='+content);
    content.load(href + " #content");
    return false; 
});

I have modified the html slightly

<div class="post">
    <h2 id="h21"><a href="other/mypage.html">My Page</a></h2>
    <div class="content">
        Teaser text1...
    </div>
</div>
<div class="post">
    <h2 id="h22"><a href="other/myotherpage.html">My Other Page</a></h2>
    <div class="content">
        Teaser text2...
    </div>
</div>
});

when I click on the first link,I get this console log output

a=[object Object]
a.get(0)=file:///home/me/dev/misc/other/mypage.html
a parent=[object Object]
a parent.get(0)=[object HTMLHeadingElement]
href=other/mypage.html
content=[object Object]

I thought the $(this) expression in the function would be the element which was clicked(ie the first <a element).Why does it appear as [object Object].I couldn't make out how a.get(0) bees file:///home/me/dev/misc/other/mypage.html

Similarly,shouldn't the variable content be equal to the first div (containing Teaser text1)? Why does it appear as [object Object]?

Share asked May 28, 2012 at 8:07 damondamon 8,48717 gold badges72 silver badges116 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

There is a difference between these two lines:

console.log(content);
console.log('content=' + content);

The first just logs the content variable. The second does a concatenation operation, concatenating the string and the object, before logging it. Concatenating a string and an object results in the object's toString method being called, which results in [object Object]. To avoid this, don't do the concatenation and just log content directly. As mindandmedia ments, you can do this by passing the values as separate arguments to console.log:

console.log('content=', content);

Finally, any native DOM link element with an href attribute does have a toString method. This returns the href value. So console.log('a.get(0)=' + a.get(0)) gets the href value of the first element in the a set.

I thought the $(this) expression in the function would be the element which was clicked(ie the first <a element).Why does it appear as [object Object].

The variable a contains a jQuery object, and the toString method isn't overridden for it, so it uses the default implementation that returns the type of the object.

I couldn't make out how a.get(0) bees file:///home/me/dev/misc/other/mypage.html

The get methods returns DOM element objects from the jQuery object, and the toString method for an <a> element returns the URL that the link points to.

Similarly,shouldn't the variable content be equal to the first div (containing Teaser text1)? Why does it appear as [object Object]?

The variable content contains a jQuery element that contains the <div> element. Using content.get(0) would give you the DOM element.

$(this) is a jQuery object, which is why it shows as [object Object]. All jQuery select functions return a jQuery object. As above, you can use .html() to get the HTML of a jQuery object, or .text() to just get the text.

It's because jQuery is chainable so you can do things like $(this).next().parent() etc etc. Each "chain" returns a jQuery object that can then have another function run on it.

Edit:

As a side note, you can use $(this)[0] to return the DOM Element I believe

try removing the strings from each of your consoles so that you only output the actual variable.

e.g. replace:

console.log('a='+a);

with

console.log(a);

and so on.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744809049a4594954.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信