javascript从div中获取html或文本 [英] javascript get html or text from div
问题描述
这在 jquery 中很容易,但是普通的 javascript 怎么样?
该代码用于 Google 图片搜索 API.我想搜索与 html 匹配的图像,或者最好是 div 中的文本.
var searchThis = document.getElementById('imgToFind');//从 div 中获取 HTML 或文本//查找图像imageSearch.execute(searchThis);//搜索图片
HTML
PlayStation
结果应该是 playstation 的图像.相反,我得到了这些
dosic ois - 123
IE8 javascript 返回 [object HTMLDivElement] 代替 [
爱心之手
以及其他不相关的图片.我想知道谷歌搜索什么......认为第二个给了我一个提示.
its var searchThis = document.getElementById('imgToFind').innerHTML;
仅提取您可以执行的文本:
element = document.getElementById('imgToFind');var searchThis = element.textContent ||element.innerText;
This is easy in jquery but how about plain javascript?
The code is for the Google image search API. I want to search images that match the html or preferably text inside a div.
var searchThis = document.getElementById('imgToFind'); //get HTML, or text from div
// Find images
imageSearch.execute(searchThis); //search image
The HTML
<div id="imgToFind">PlayStation</div>
The result should be images of playstation. Instead I get these
dosic ois - 123
IE8 javascript returns [object HTMLDivElement] instead [
Loving Hand
And other irrelevant images. I wonder what Google searches for.. think the second one is giving me a hint.
its var searchThis = document.getElementById('imgToFind').innerHTML;
to extract only text you can do:
element = document.getElementById('imgToFind');
var searchThis = element.textContent || element.innerText;
这篇关于javascript从div中获取html或文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!