jQuery 如何通过jQuery中的类获取特定html元素的innerHTML?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11666605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:49:20 来源:igfitidea点击:
How to get innerHTML for a particular html element through its class in jQuery?
提问by EbinPaulose
I have HTML code like this:
我有这样的 HTML 代码:
<div class="a">html value 1</div>
<div class="a">html value 2</div>
How can I access html value 1
and html value 2
using jquery?
如何访问html value 1
和html value 2
使用 jquery?
回答by thecodeparadox
Separately:
分别地:
$('div.a:eq(0)').html(); // $('div.a:eq(0)').text();
$('div.a:eq(1)').html(); // $('div.a:eq(1)').text();
Using loop:
使用循环:
$('div.a').each(function() {
console.log( $(this).html() ); //or $(this).text();
});
Using .html()
使用 .html()
?$('div.a').html(function(i, oldHtml) {
console.log( oldHtml )
})??;
Using .text()
使用 .text()
$('div.a').text(function(i, oldtext) {
console.log( oldtext )
})?;
回答by undefined
回答by MrUpsidown
Based only on the class as he did ask:
仅基于他所问的课程:
$('.a').each(function() {
console.log($(this).html());
});