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?

jqueryhtmlinnerhtml

提问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 1and html value 2using jquery?

如何访问html value 1html value 2使用 jquery?

采纳答案by adeneo

$('.a')[0].innerHTML;
$('.a')[1].innerHTML;

FIDDLE

小提琴

回答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 )
})??;

DEMO

演示

Using .text()

使用 .text()

$('div.a').text(function(i, oldtext) {
  console.log( oldtext )
})?;

DEMO

演示

回答by undefined

Try this:

尝试这个:

var a = document.getElementsByClassName('a');
for (var i = 0; i < a.length; i++) {
    alert(a[i].innerHTML)
}

demo

演示

回答by MrUpsidown

Based only on the class as he did ask:

仅基于他所问的课程:

$('.a').each(function() {
    console.log($(this).html());
});