<分区>
我正在尝试获取“li”元素的 html 并将它们作为字符串提醒。
HTML:
<ul>
<li>lorem ipsum dolor <span>sit</span> amet 1</li>
<li>lorem ipsum dolor <span>sit</span> amet 2</li>
<li>lorem ipsum dolor <span>sit</span> amet 3</li>
<li>lorem ipsum dolor <span>sit</span> amet 4</li>
<li>lorem ipsum dolor <span>sit</span> amet 5</li>
<li>lorem ipsum dolor <span>sit</span> amet 6</li>
</ul>
<p id="greeting">Hello <strong>World</strong>!</p>
Javascript:
$(document).ready(function(e){
var list = $("li");
var greeting = $("#greeting");
var content ="";
for(var i=0, len = list.length; i<len; i++) {
content += $(list[i]).html();
//why do I have to use the $ here instead of just doing list[i].html()
//content += list[i].html(); why does this not work?
}
alert(content);
alert(greeting.html()); //why does this work without the $ ?
});
我做了一些研究,了解到 jQuery 选择器返回包裹在 jQuery 对象中的 DOM 元素,这样我们就可以在其上应用 jQuery 方法,但是为什么 greeting.html() 行在没有 $?
TypeError: list[i].html 不是函数
为什么当我执行 list[i].html 而不是 $(list[i]).html() 时会出现此错误?
这是 fiddle .