简体   繁体   中英

How to retrieve html content and display it elsewhere with jQuery?

I'm working on a skill-improving personal project which is a simple web calendar. I'm fairly new to jQuery so I made this project to improve my skills with it. I want to explain what I'm doing in detail so you understand what my problem is.

I have 31 h1's with spans inside that get their content through jQuery

<h1 class="number"><span></span></h1> *31

var date = 1;
$('.number').each(function() {
    $(this).find('span').html(date);
    date++;
});

I can select individual dates from the list with this script:

$('.number').click(function() {
    $(this).toggleClass("selected")
        .siblings('.selected').removeClass("selected");
});

And now I have a different h1 (#big_date) elsewhere on the page that I would like to feed with the selected date, ie when I select the 12th in the list I want #big_date to display 12. How would I do that? Thanks for the help in advance !

You can use jQuery's .text() function :

 $('.number').click(function() { $(this).toggleClass("selected") .siblings('.selected').removeClass("selected"); $('#big_date').text( $('.selected').text() ); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <style>body{font-family:Arial,Helvetica,sans-serif}#big_date{background:#1d89fd;height:1.2em;line-height:1.3em;text-align:center;width:2em;color:#fff}.number{display: inline-block;margin:0;width:1.5em;height:1.5em;line-height:1.5em;text-align:center;padding:.5em;font-size:16px;border:1px solid #ddd;cursor:pointer;-webkit-transition:all .15s ease;-moz-transition:all .15s ease;transition:all .15s ease}.number:hover{background:#eee}.selected{background: #555!important; color: #fff}.selected:hover{background: #666!important;}</style> <h1 id="big_date"></h1> <h1 class="number"><span>1</span></h1><h1 class="number"><span>2</span></h1><h1 class="number"><span>3</span></h1><h1 class="number"><span>4</span></h1><h1 class="number"><span>5</span></h1><h1 class="number"><span>6</span></h1><h1 class="number"><span>7</span></h1><h1 class="number"><span>8</span></h1><h1 class="number"><span>9</span></h1><h1 class="number"><span>10</span></h1><h1 class="number"><span>11</span></h1><h1 class="number"><span>12</span></h1><h1 class="number"><span>13</span></h1><h1 class="number"><span>14</span></h1><h1 class="number"><span>15</span></h1><h1 class="number"><span>16</span></h1><h1 class="number"><span>17</span></h1><h1 class="number"><span>18</span></h1><h1 class="number"><span>19</span></h1><h1 class="number"><span>20</span></h1><h1 class="number"><span>21</span></h1><h1 class="number"><span>22</span></h1><h1 class="number"><span>23</span></h1><h1 class="number"><span>24</span></h1><h1 class="number"><span>25</span></h1><h1 class="number"><span>26</span></h1><h1 class="number"><span>27</span></h1><h1 class="number"><span>28</span></h1><h1 class="number"><span>29</span></h1><h1 class="number"><span>30</span></h1><h1 class="number"><span>31</span></h1>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM