简体   繁体   中英

How to get the inner html value

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

<p>
  <span style="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

这不起作用,你做一个getElementById,但你没有任何带有Id的元素......

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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