我正在开发一项技能改进的个人项目,这是一个简单的网络日历。我是jQuery的新手,所以我做了这个项目来提高我的技能。我想解释一下我在做什么,以便你了解我的问题。
我有31个带有跨度的h1通过jQuery获取其内容
<h1 class="number"><span></span></h1> *31
var date = 1;
$('.number').each(function() {
$(this).find('span').html(date);
date++;
});
我可以使用以下脚本从列表中选择个别日期:
$('.number').click(function() {
$(this).toggleClass("selected")
.siblings('.selected').removeClass("selected");
});
现在我在页面的其他地方有一个不同的h1(#big_date)我希望用所选日期提供,即当我选择列表中的第12个时我想要#big_date显示12.我该怎么做那? 感谢您的帮助!
答案 0 :(得分:1)
您可以使用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>