简体   繁体   English

用于获取网页内容的Javascript?

[英]Javascript for fetching the contents of an web page?

I want to fetch the value of class .cell-hover through javascript and it should print the value which is present in double quotes. 我想通过javascript提取.cell-hover类的值,它应该打印用双引号引起来的值。 So far I tried: 到目前为止,我尝试了:

var phonenumbers = document.getElementsByClassName('cell-hover');
document.write(phonenumbers);

but its printing [object HTMLCollection] , and I want to print the exact value. 但其打印[object HTMLCollection] ,我想打印确切的值。

Attached Snapshot: 所附快照:

控制台日志截图

document.getElementsByClassName('cell-hover'); will return an array of elements with class name cell-hover .Loop through that and print the result.For the content inside that class use innerHTML . 将返回一个类名称为cell-hover的元素数组。循环遍历并打印结果。对于该类中的内容,请使用innerHTML The innerHTML will return DOM content as a String innerHTML将以字符串形式返回DOM内容

You can also use node.textContent; 您也可以使用node.textContent; if you know that your div contains only text Here in the snippet i have printed the content of the first element with class name cell-hover 如果您知道div仅包含文本,则在此代码段中,我已打印了类名称为cell-hover的第一个元素的内容

 var phonenumbers = document.getElementsByClassName('cell-hover'); document.write( phonenumbers[0].innerHTML); 
 <div class="cell-hover"> 2637145689 </div> 

  var phonenumbers = document.getElementsByClassName('cell-hover'); for(var i=0; i < phonenumbers.length ; i++){ document.write( phonenumbers[0].textContent ); } 
 <div class="cell-hover"> 2637145689 </div> 

一班轮:

document.write(document.querySelector('.cell-hover').innerHTML);

document.write(document.querySelector('.cell-hover').innerText);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM