奇怪的jQuery $ .each()行为

TMP*_*lot 0 javascript jquery html-lists

我在.each()没有拿起某些text/html时遇到了奇怪的问题.

我有:

<ul class="errors">
   <li class="title">Please check the details you entered and try again.</li>
   <li id="title">Please supply a title</li>
   <li id="typeID">Please select a type</li>
   <li id="availID">Please select the availablilty</li>
   <li id="city">Please supply a valid city</li>
   <li id="postcode">Please supply a valid postcode</li>
   <li id="contactNum">Please supply a valid contact number</li>
   <li id="description">Please enter a description</li>
</ul>
Run Code Online (Sandbox Code Playgroud)

对于jquery:

$('.errors').children('^[id]').each(function(index){
    alert(index + ': ' + $(this).text());
});
Run Code Online (Sandbox Code Playgroud)

现在的问题是,jquery没有为"description"列表项选取text/html.无论我尝试什么.

有关为什么会发生这种情况的任何想法?

谢谢,TMP

更新:我设法找到了问题.事实上,页面上有另一个具有相同ID的元素使其停止工作.奇怪的是,因为所有其他列表项都是相同的但工作得很好.也许是个bug?

干杯,TMP

Col*_*ock 5

alert(index + ': ' + $(this.text());
Run Code Online (Sandbox Code Playgroud)

应该

alert( index + ': ' + $(this).text() );
Run Code Online (Sandbox Code Playgroud)

你错过了一个右括号this.

编辑

修正了错字后,我得到了预期的结果:http://jsfiddle.net/xQyAt/