开发者

How to get the inner html value

var str_f = document.getElementById("" + j + "").innerHTML;

<p>
  <span styl开发者_开发知识库e="color: red">advertising cctv/bust</span>
  <span style="color: red">a</span>tion
</p>

how can i get the value


If you want to get the innerHTML of the <p> tag then give it an id and then use the following code

<p id="para1"><span style="color: red"> advertising cctv/bust</span><span style="color: red">a</span>tion </p>


document.getElementById("para1").innerHTML;

If you want to get text content inside the element then you can use

var elem = document.getElementById("para1");
var elementText = x.innerText || x.textContent


This won't work, your doing a getElementById but you don't have any elements with Id's...


Firstly, to get the innerHTML value of any tag, you either need that tag to have its 'id' property or 'name' property set.

Then you can respectively use the 'document.getElementById(yourTagIdValue).innerHTML' or 'document.getElementByName(yourTagNameValue).innerHTML' to fetch the value of the required tag.

For eg.

Sampath

So, you can get the innerHTML of this tag as follows:

document.getElementById("lblName").innerHTML.

Also, do kindly note that there is a difference for HTML tags for which you can use the 'innerHTML' property and 'value' property.

Regards, Mahendra Liya.


You haven't mention the ID anywhere in the Tags so you cannot use getElementsById.

You can get the innerHTML of the < p > by using getElementsByTagName.

Here is how we use it. Syndtax: var elements = document.getElementsByTagName(name);

In your case:

var pElement = document.getElementsByTagName('p')[0];

var pElementHtml = pElement.innerHTML;

if you need just the text, you can do

var pElementText = pElement.innerText || pElement.textContent;

Hope its useful for someone.

-Sijan

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜