开发者

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

开发者 https://www.devze.com 2022-12-22 11:47 出处:网络
I have this piece of code: <li class=\"selected\"><a href=\"/AmIACandidate.html\">Am I a Candidate?</a></li>

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

    精彩评论

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

    关注公众号