简体   繁体   English

如何获取内部html值

[英]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>标记的innerHTML,请为其提供一个ID,然后使用以下代码

<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. 首先,要获取任何标签的innerHTML值,您需要该标签设置其'id'属性或'name'属性。

Then you can respectively use the 'document.getElementById(yourTagIdValue).innerHTML' or 'document.getElementByName(yourTagNameValue).innerHTML' to fetch the value of the required tag. 然后,您可以分别使用'document.getElementById(yourTagIdValue).innerHTML'或'document.getElementByName(yourTagNameValue).innerHTML'来获取所需标签的值。

For eg. 例如。

Sampath 萨姆帕斯

So, you can get the innerHTML of this tag as follows: 因此,您可以按如下方式获取此标记的innerHTML:

document.getElementById("lblName").innerHTML. 的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. 另外,请注意HTML标记也有所不同,您可以使用它们的'innerHTML'属性和'value'属性。

Regards, Mahendra Liya. 此致,Mahendra Liya。

You haven't mention the ID anywhere in the Tags so you cannot use getElementsById. 您没有在标签的任何地方提及ID,因此无法使用getElementsById。

You can get the innerHTML of the < p > by using getElementsByTagName . 您可以使用getElementsByTagName获得<p>的innerHTML。

Here is how we use it. 以下是我们如何使用它。 Syndtax: var elements = document.getElementsByTagName(name); 语法:var elements = document.getElementsByTagName(name);

In your case: 在你的情况下:

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

var pElementHtml = pElement.innerHTML; var pElementHtml = pElement.innerHTML;

if you need just the text, you can do 如果您只需要文字,则可以

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

Hope its useful for someone. 希望它对某人有用。

-Sijan -Sijan

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

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