简体   繁体   English

如何从节点列表中访问HTML元素的内容?

[英]How can I access the content of an HTML element from a node list?

I can't seem to access the content of certain elements and I think it must be because of the syntax I'm using. 我似乎无法访问某些元素的内容,我认为它必须是因为我正在使用的语法。 I'm trying to write and overwrite members of a getElementsByClassName list. 我正在尝试编写并覆盖getElementsByClassName列表的成员。 I understand that it is a nodeList and not an array, but I still can't figure it out. 我知道它是一个nodeList而不是一个数组,但我仍然无法弄明白。 Here's what I have/have tried: 这是我已经/尝试过的:

HTML: HTML:

<span class="myclass">Text</span>
<span class="myclass">Text2</span>

JS: JS:

var spanarray = document.getElementsByClassName("myclass");
//I've tried all of the following (for the record, I thought either 2 or 5 would work)
spanarray[0] = "Replacement text";
spanarray[0].innerHTML = "Replacement text";
spanarray.item(0) = "Replacement text";
spanarray.item(0).innerHTML = "Replacement text";
spanarray.item(0).nodeValue = "Replacement text";

I'd really appreciate if somebody could point me in the right direction. 如果有人能指出我正确的方向,我真的很感激。 Thanks. 谢谢。

The following (your 2nd example) works: 以下(您的第二个示例)有效:

var spanarray = document.getElementsByClassName("myclass");
spanarray[0].innerHTML = "ABC"

Here's a jsfiddle and try in your browser: http://jsfiddle.net/N4fjX/ . 这是一个jsfiddle,并在您的浏览器中尝试: http//jsfiddle.net/N4fjX/

Post the remainder of your page if you've got further issues. 如果您还有其他问题,请发布您网页的其余部分。

Judging from your comments, it might be the case where your Javascript is being processed before your entire page is rendered. 从您的评论来看,可能是在整个页面呈现之前处理您的Javascript的情况。

To make sure it has rendered, try 为了确保它已渲染,请尝试

window.onload = setText(); 
function setText()
{
    var spanarray = document.getElementsByClassName("myclass");
    spanarray[0].innerHTML = "Replacement text";
}

It turns out that my problem was the PHP being called from an external JS function via AJAX, so while the PHP-generated content showed up in the browser, the elements were not actually visible under "View Source" and therefore not accessible by the JS. 事实证明我的问题是通过AJAX从外部JS函数调用PHP,所以当PHP生成的内容出现在浏览器中时,元素在“查看源代码”下实际上不可见,因此JS无法访问。 I should have mentioned the PHP part in the original post but I didn't think it mattered. 我应该在原帖中提到PHP部分,但我认为这不重要。

DOESN'T WORK: 不工作:

loadText.php loadText.php

<?php
echo "<span class='myclass'>This text should be replaced</span>"
?>

HTML HTML

<div id='somediv'>
  <script type="text/javascript">loadText();</script>
</div>
...
<script type="text/javascript">
  var spanarray = document.getElementByClassName("myclass"); //doesn't work in IE6-9
  spanarray[0].innerHTML = "Replacement Text";
</script>

JS JS

function loadText(){
  var somediv = document.getElementById("somediv");
  var xmlhttp;
  if (window.XMLHttpRequest)
  {// for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  }
  else
  {// for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange = function()
  {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
      somediv.innerHTML = xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","loadText.php",true);
  xmlhttp.send();
}

SOLUTION: 解:

Instead of jumping through extra loops with AJAX, execute the PHP directly on the HTML page. 不要使用AJAX跳过额外的循环,而是直接在HTML页面上执行PHP。 If AJAX is absolutely necessary, then I'm not really sure what the solution would be. 如果AJAX是绝对必要的,那么我不确定解决方案是什么。 Plymouth223 and Jonathan Chow have provided the correct JS syntax. Plymouth223和Jonathan Chow提供了正确的JS语法。

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

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