开发者

javascript get html or text from div

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开发者_Go百科 I get these

javascript get html or text from div

dosic ois - 123

javascript get html or text from div

IE8 javascript returns [object HTMLDivElement] instead [

javascript get html or text from div

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;


var searchThis = document.getElementById('imgToFind').innerHTML;

will grab the contents of the element :)


If you want just the text within an element, you can use textContent in the more standards compliant browsers (i.e. not IE) and innerText in IE. They're not identical in all cases but for your purposes they may well be.

var el = document.getElementById("foo");
var text = el.textContent || el.innerText;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜