开发者

Need to copy the text inside an <a> tag and move it inside a specific <div>

I have this piece of code:

<li class="selected"><a href="/AmIACandidate.html">Am I a Candidate?</a></li>

I need to grab the text "Am I a Candidate?"开发者_如何学Go from inside the tag and move it inside a specific div tag.

<div id="sideNavHeader"> --Copy needs to go here-- </div>

I can copy the whole

  • but can't seem to figure out how to grab just the text. I'm using it to dynamically create a title to a side nav on my site.

    Thanks for any help


    You probably want to use .text():

    $('#sideNavHeader').text($('li.selected a').text());
    


    Tobias Cohen's selector was much better than mine you should use:

    $('#sideNavHeader').html($('li.selected a').html());
    

    instead of my original code:

    $('#sideNavHeader').html($('li.selected').children('a').html());
    
  • 0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新问答

    问答排行榜