简体   繁体   English

Javascript按类名获取内部HTML文本

[英]Javascript get inner HTML text for span by class name

This is the basic format of the code the table is contained within a div named 这是表包含在名为div的代码的基本格式

<div class="leftCol">    
.....
<tr id="my_cd">
<td><span class="agt_span">My Code</span></td>
</tr>
.....
</div>

I need to be able to get whatever text is contained within the span class, in this case I need to pull the text "My Code" and then add that into an array. 我需要能够获得span类中包含的任何文本,在这种情况下,我需要将文本“My Code”拉出来,然后将其添加到数组中。 Adding the text into an array is not the issue that's easy but I can't figure out how to pull the text. 将文本添加到数组中不是容易的问题,但我无法弄清楚如何拉文本。 No matter what I try I can't get anything but an 'undefined' value. 无论我尝试什么,我都得不到任何东西,除了'未定义'的价值。

How do I get the Inner HTML text value for a span by class name? 如何按类名获取跨度的内部HTML文本值?

First Question solved thanks!! 第一个问题解决了!

Second question expand on first: 第二个问题首先扩展:

<div class="leftCol">    
.....
<tr id="my_cd">
  <td><span class="agt_span">My Code</span></td>
  <td>
    <div>
      <select name="agt_drp" id="agt_drp" class="agt_drp">...</select>
    </div>
  </td>
</tr>
</div>

Let's say I have the select id "agt_drp" and I want to get the span class text. 假设我有选择ID“agt_drp”,我想得到span类文本。 Is there any way to do that? 有没有办法做到这一点?

Jquery: jQuery的:

var test = $("span.agt_span").text();
alert(test):

Javascript: http://www.w3schools.com/jsref/met_document_getelementsbyclassname.asp Javascript: http//www.w3schools.com/jsref/met_document_getelementsbyclassname.asp

in vanilla javascript, you can use getElementsByClassName(): 在vanilla javascript中,您可以使用getElementsByClassName():

var htmlString = document.getElementsByClassName('agt_span')[0].innerHTML;

https://jsfiddle.net/ky38esoo/ https://jsfiddle.net/ky38esoo/

Notice the index behind the method. 注意方法背后的索引。

JQuery: JQuery的:

$('span.agt_span').text();

Pure JavaScript (you need to specify the position of your class element: [0] to get the first one): 纯JavaScript(你需要指定你的类元素的位置: [0]来获得第一个):

document.getElementsByClassName('agt_span')[0].innerHTML;

If you have multiples elements with this class, you can loop on it: 如果你有这个类的倍数元素,你可以循环它:

var elts = document.getElementsByClassName('agt_span');
for (var i = 0; i < elts.length; ++i) {
    alert(elts[i].innerHTML);
}

Though getElementsByClassName seems to be supported by all major browser, that is now argument to use it. 虽然getElementsByClassName似乎得到了所有主流浏览器的支持,但现在是使用它的参数。 To keep your code compatible and usefull, better use the W3C Standard DOM Level 3 Core . 为了使您的代码兼容且有用,请更好地使用W3C标准DOM Level 3 Core The Document IDL does not describe such a method there! Document IDL没有描述这样的方法!

So please use 所以请使用

var table = document.getElementById("my_cd"); /* id is unique by definition! */
var spans = table.getElementsByTagName("span");
var txt;

for(i in spans) {
    if(spans[i].getAttribute("class").contains("agt_span")){
        txt = spans[i].firstChild; /* a span should have only one child node, that contains the text */
    }
}
return txt;

This method isn't perfect, as you actually need to split the spans[i].getAttribute("class").split(" ") on space chars and check if this array contains "agt_span". 这个方法并不完美,因为你实际上需要在空格字符上拆分spans[i].getAttribute("class").split(" ")并检查这个数组是否包含“agt_span”。

By the way: innerHTML is no DOM Attribute too. 顺便说一句:innerHTML也没有DOM属性。 But you can implement anything in a compatible and flexible way using W3C DOM and you will be sure to write effective and compatible code. 但是您可以使用W3C DOM以兼容且灵活的方式实现任何内容,并且您将确保编写有效且兼容的代码。

If the js programmers had used the W3C Documents and if there weren't no Internet Explorer to break all those ECMAScript and W3C rules, there would never have been that many incompatibilities between all those browser versions. 如果js程序员使用过W3C文档,并且如果没有Internet Explorer来破坏所有这些ECMAScript和W3C规则,那么所有这些浏览器版本之间就不会有那么多不兼容性。

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

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