jQuery复制选择器错误

前端之家收集整理的这篇文章主要介绍了jQuery复制选择器错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前正试图用6个可点击的电子表格设置一个表格,允许输入框出现,以便您可以添加注释,但是我收到重复的jQuery选择器错误,并且通过调试我的第二个函数我发现.html()是也不工作。这是我的6个函数代码当单击特定单元格时,每个都被调用
$("#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”);.

解决方法

没有这样的重复的jQuery选择器错误;这是IntelliJ(和其他IDE从WebStorm …等想法)抛出的警告,表明您应该将jQuery选择存储在局部变量中,而不是重复选择。

摘自jQuery documentation

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选择,所以我敢打赌,警告是从别的地方来的。什么是符合这个事实,添加后丢失的$仍然存在。

一般来说,一个很好的做法是添加报告的错误到你的问题。

猜你在找的jQuery相关文章