[英]Getting text in nested html tag using jquery
I am trying to print the first ten entries of a website which has a list, and I want the result in the console itself. 我正在尝试打印具有列表的网站的前十个条目,并且我希望结果显示在控制台本身中。 Here's the structure of the HTML it's using
这是它使用的HTML的结构
<div class=classname>
<b>The text i want</b>
</div>
here's what i tried running in the console: 这是我尝试在控制台中运行的内容:
list = document.getElementsByClassName('classname')
for(var i=0;i<10;i++)
{
console.log (list[i])
}
I am at a loss of what to do next. 我不知道下一步该怎么做。 I'm fairly new to javascript so any help would be greatly appreciated.
我对javascript还是很陌生,所以将不胜感激。
You can use the innerText
property to get the text. 您可以使用
innerText
属性获取文本。
Also, use list.length
based comparison for running the for loop instead of arbitrary numbers such as 10
: for(var i=0;i<10;i++)
. 另外,使用基于
list.length
的比较来运行for循环,而不要使用诸如10
: for(var i=0;i<10;i++)
类的任意数字。
list = document.getElementsByClassName('classname') for(var i=0;i < list.length;i++) { console.log(list[i].innerText); }
<div class="classname"> <b>The text i want</b> </div>
Keep in mind that unlike textConten
, innerText
will not display any text that is not visible on screen, or text inside <script>
tags. 请记住,与
textConten
不同, innerText
不会显示屏幕上不可见的任何文本,也不会显示<script>
标记内的文本。 See this for more info on the differences. 有关差异的更多信息, 请参见此内容。
Use textContent
or innerText
to get the text you want. 使用
textContent
或innerText
获取所需的文本。 list[i].textContent
You can also use .innerHTML
this will also show you tags. 您也可以使用
.innerHTML
这也会向您显示标签。
Demo 演示版
list = document.getElementsByClassName('classname') for(var i=0;i<list.length;i++) { console.log (list[i].textContent) }
<div class=classname> <b>The text i want</b> </div>
First thing is you need to use textContent to get the text content inside the selected element node. 首先,您需要使用textContent来获取所选元素节点内的文本内容。
Also to make it simpler to target nested elements in JavaScript, you can use these functions: 另外,为了更轻松地在JavaScript中定位嵌套元素,您可以使用以下函数:
querySelectorAll querySelectorAll
var myTags = document.querySelectorAll(".classname b"); for( var i = 0; i < myTags.length; i++ ){ var curTag = myTags[i]; console.log( curTag.textContent ); }
<div class=classname> <b>The text i want</b> </div> <div class=classname> <b>The text i want (2nd time)</b> </div> <div class=classname> <b>The text i want (3rd time)</b> </div>
with jQuery, it's simple: 使用jQuery,很简单:
$('.classname').each(function(i,e) {
if (i==10) return false;
console.log($(e).text());
});
$('.classname').each(function(i,e) { if (i==10) return false; console.log($(e).text()); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class=classname> <b>The text i want 1</b> </div> <div class=classname> <b>The text i want 2</b> </div> <div class=classname> <b>The text i want 3</b> </div> <div class=classname> <b>The text i want 4</b> </div> <div class=classname> <b>The text i want 5</b> </div> <div class=classname> <b>The text i want 6</b> </div> <div class=classname> <b>The text i want 7</b> </div> <div class=classname> <b>The text i want 8</b> </div> <div class=classname> <b>The text i want 9</b> </div> <div class=classname> <b>The text i want 10</b> </div> <div class=classname> <b>The text i want 11</b> </div> <div class=classname> <b>The text i want 12</b> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.