我有这个日历的HTML
<table class="homepagemodule report" id="monthly_calendar" align="center" cellspacing="1">
<caption>
<span>Monthly Calendar</span>
</caption>
<tbody>
<tr class="month_header"><th colspan="5">
<a href="http://football21.myfantasyleague.com/2013/options?L=24779&O=123&MONTH=11&YEAR=2013" title="Previous Month"><</a> Jan 2014 <a href="http://football21.myfantasyleague.com/2013/options?L=24779&O=123&MONTH=1&YEAR=2014" title="Next Month">></a></th><th colspan="2"><select name="LOCATION" size="1" onchange="self.location=this.options[this.selectedIndex].value"><option>Jump To...</option>
<option value="http://football21.myfantasyleague.com/2013/options?L=24779&O=123&MONTH=4&YEAR=2013">May 2013</option>
<option value="http://football21.myfantasyleague.com/2013/options?L=24779&O=123&MONTH=5&YEAR=2013">Jun 2013</option>
<option value="http://football21.myfantasyleague.com/2013/options?L=24779&O=123&MONTH=6&YEAR=2013">Jul 2013</option>
<option value="http://football21.myfantasyleague.com/2013/options?L=24779&O=123&MONTH=7&YEAR=2013">Aug 2013</option>
<option value="http://football21.myfantasyleague.com/2013/options?L=24779&O=123&MONTH=8&YEAR=2013">Sep 2013</option>
<option value="http://football21.myfantasyleague.com/2013/options?L=24779&O=123&MONTH=9&YEAR=2013">Oct 2013</option>
<option value="http://football21.myfantasyleague.com/2013/options?L=24779&O=123&MONTH=10&YEAR=2013">Nov 2013</option>
<option value="http://football21.myfantasyleague.com/2013/options?L=24779&O=123&MONTH=11&YEAR=2013">Dec 2013</option>
<option value="http://football21.myfantasyleague.com/2013/options?L=24779&O=123&MONTH=0&YEAR=2014">Jan 2014</option>
<option value="http://football21.myfantasyleague.com/2013/options?L=24779&O=123&MONTH=1&YEAR=2014">Feb 2014</option>
<option value="http://football21.myfantasyleague.com/2013/options?L=24779&O=123&MONTH=2&YEAR=2014">Mar 2014</option>
<option value="http://football21.myfantasyleague.com/2013/options?L=24779&O=123&MONTH=3&YEAR=2014">Apr 2014</option>
</select></th>
</tr><tr class="day_of_week_header"><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr><tr class="oddtablerow"><td> </td>
<td> </td>
<td> </td>
<td valign="top"><div class="calendarday">1 <a href="http://football21.myfantasyleague.com/2013/options?L=24779&O=110&MONTH=0&DAY=1&YEAR=2014" title="Create a New Event On This Day">+</a></div></td>
<td valign="top"><div class="calendarday">2 <a href="http://football21.myfantasyleague.com/2013/options?L=24779&O=110&MONTH=0&DAY=2&YEAR=2014" title="Create a New Event On This Day">+</a></div></td>
</tr><tr class="eventablerow"><td valign="top"><div class="calendarday">5 <a href="http://football21.myfantasyleague.com/2013/options?L=24779&O=110&MONTH=0&DAY=5&YEAR=2014" title="Create a New Event On This Day">+</a></div></td>
<td valign="top"><div class="calendarday">13 <a href="http://football21.myfantasyleague.com/2013/options?L=24779&O=110&MONTH=0&DAY=13&YEAR=2014" title="Create a New Event On This Day">+</a></div></td>
<td valign="top"><div class="calendarday">14 <a href="http://football21.myfantasyleague.com/2013/options?L=24779&O=110&MONTH=0&DAY=14&YEAR=2014" title="Create a New Event On This Day">+</a></div></td>
<td class="today" valign="top"><div class="calendarday">15 <a href="http://football21.myfantasyleague.com/2013/options?L=24779&O=110&MONTH=0&DAY=15&YEAR=2014" title="Create a New Event On This Day">+</a></div><ul><li class="calendar_event_trade">No Trades Allowed starts at 12:00 p.m. (<a href="http://football21.myfantasyleague.com/2013/league_calendar_setup?L=24779&EVENT_ID=3126760&ACTION=DELETE&INSTANCE=1389805200" title="Delete This Calendar Event" onclick="return confirm('You are now deleting this league calendar event.\nAre you sure that is what you want to do?');">X</a>)</li>
</ul></td>
<td valign="top"><div class="calendarday">16 <a href="http://football21.myfantasyleague.com/2013/options?L=24779&O=110&MONTH=0&DAY=16&YEAR=2014" title="Create a New Event On This Day">+</a></div></td>
</tr></tbody></table>
我正在尝试从这个html中取出“今天”的td类并将其放在我页面上其他地方的另一个div中,这样我就不必显示完整的日历,只显示今天的事件。我已经尝试过将这个jquery附加到另一个div但它无效。
<script type="text/javascript">
$('#monthly_calendar td.today').appendTo('#todayevents');
</script>
<div id="todayevents"></div>
答案 0 :(得分:0)
您的代码在文档(DOM)准备好之前正在运行。你的代码很好,但因为它运行得太早,它无法正常工作。试试这个:
<script type="text/javascript">
$( document ).ready(function() {
$('#monthly_calendar td.today').appendTo('#todaycalendar');
});
</script>
在文档“准备就绪”之前,无法安全地操作页面。 jQuery为您检测这种准备状态。代码包含在里面 $(document).ready()只会运行一次页面Document Object Model(DOM)已准备好执行JavaScript代码。代码包括在内 在$(window).load(function(){...})里面会运行整个 页面(图片或iframe),而不仅仅是DOM,已准备就绪。
// A $( document ).ready() block.
$( document ).ready(function() {
console.log( "ready!" );
});
答案 1 :(得分:0)
尝试将代码包装在DOM中以使其正常工作
$(document).ready(function(){
$('#monthly_calendar td.today').appendTo('#todayevents');
});
答案 2 :(得分:0)
在document.ready
中编写代码:
$( document ).ready(function() {
$('#monthly_calendar td.today').appendTo('#todayevents');
});
答案 3 :(得分:0)
试试这个DEMO,只需将文档准备好添加到脚本中,以便在DOM准备好后执行脚本。
$(document).ready(function(){
$('#monthly_calendar td.today').appendTo('#todaycalendar');
});