$("#mondayCommentLink").click(function (){ var mondayhtmls = $("#mondayComment"); var input = $("<input type='text' id='mondayCommentText' name='mondayCommentText' />"); input.val(data.days[0].comment); mondayhtmls.html(input); }); $("#tuesdaysCommentLink").click(function (){ var tuesdayhtmls = ("#tuesdayComment"); var inputt = $("<input type='text' id='tuesdayCommentText' name='tuesdayCommentText' />"); inputt.val(data.days[1].comment); tuesdayhtmls.html("test"); }); $("#wednesdayCommentLink").click(function (){ var htmls = ("#wednesdayComment"); var input = $("<input type='text' id='wednesdayCommentText' name='wednesdayCommentText' />"); input.val(data.days[2].comment); htmls.html(input); }); $("#thursdayCommentLink").click(function (){ var htmls = ("#thursdayComment"); var input = $("<input type='text' id='thursdayCommentText' name='thursdayCommentText' />"); input.val(data.days[3].comment); htmls.html(input); }); $("#fridayCommentLink").click(function (){ var htmls = ("#fridayComment"); var input = $("<input type='text' id='fridayCommentText' name='fridayCommentText' />"); input.val(data.days[4].comment); htmls.html(input); }); $("#saturdayCommentLink").click(function (){ var htmls = ("#saturdayComment"); var input = $("<input type='text' id='saturdayCommentText' name='saturdayCommentText' />"); input.val(data.days[5].comment); htmls.html(input); });
这是他们从哪里来的:
<th id="mondayComment" name="mondayComment" style="text-align: center; width: 115px;"><div id="mondayCommentLink">+</div></th> <th id="tuesdayComment" name="tuesdayComment" style="text-align: center; width: 115px;"><div id="tuesdaysCommentLink">+</div></th> <th id="wednesdayComment" name="wednesdayComment" style="text-align: center; width: 115px;"><div id="wednesdayCommentLink">+</div></th> <th id="thursdayComment" name="thursdayComment" style="nowrap; text-align: center; width: 115px;"><div id="thursdayCommentLink">+</div></th> <th id="fridayComment" name="fridayComment" style="text-align: center; width: 115px;"><div id="fridayCommentLink">+</div></th> <th id="saturdayComment" name="saturdayComment" style="text-align: center; width: 115px;"><div id="saturdayCommentLink">+</div></th>
我不明白为什么我在#mondayCommentLink,#tuesdayCommentLink等上得到一个重复的选择器错误。有没有我错过或错误地做错了?第一个单元格工作,我可以点击它,一个输入框将弹出,但它失败的第二个单元格#tuesdayCommentLink在tuesday.htmls.html(“test”);.
解决方法
Saving Selections
jQuery doesn’t cache elements for you. If you’ve made a selection that you might need to make again,you should save the selection in a variable rather than making the selection repeatedly.
1| var divs = $( "div" );
Once the selection is stored in a variable,you can call jQuery methods on the variable just like you would have called them on the original selection.
A selection only fetches the elements that are on the page at the time the selection is made. If elements are added to the page later,you’ll have to repeat the selection or otherwise add them to the selection stored in the variable. Stored selections don’t magically update when the DOM changes.
然而,在你的代码中,没有重复的jQuery选择,所以我敢打赌,警告是从别的地方来的。什么是符合这个事实,添加后丢失的$仍然存在。