简体   繁体   English

为什么getElementsByClassName不起作用?

[英]Why getElementsByClassName does not work?

I'm trying to detect screen resolution then set position (like top,left) for multiple screen resolution,but it's not working. 我正在尝试检测屏幕分辨率,然后为多个屏幕分辨率设置位置(例如顶部,左侧),但是它不起作用。 Anyone knows what's wrong with my code? 有人知道我的代码有什么问题吗?

CSS: CSS:

.Scrolloutside
{
  position:relative;
  left: 550px;
 }

javascript: JavaScript的:

  var nHeight = screen.height;
  var nWidth = screen.width; 
  if (nHeight ==714 && nWidth==1005)
 {
   //document.write("height:"+nHeight+" ,width="+nWidth+"<br>");
   var newsTarget = document.getElementsByClassName('Scrolloutside');
   newsTarget.style.top= "500px";
  }

html: HTML:

<div class = "Scrolloutside">
    <div class="scroller_title">News</div>
<div class="scroller_container">
    <div class="jscroller2_up">
    <?
    echo $secnews;
    ?>
    </div>
</div>
</div>
</div>

document.getElementsByClassName() returns a nodeList or HTMLCollection which are both a list of elements, not a single element. document.getElementsByClassName()返回一个nodeListHTMLCollection ,它们都是元素列表,而不是单个元素。 Even if there is only one matching element, it still returns a list with just one item in it. 即使只有一个匹配元素,它仍会返回仅包含一项的列表。 As such, you have to either get the first item in the list or iterate through the whole list (depending upon what your code wants). 因此,您必须获取列表中的第一项,或者遍历整个列表(取决于您的代码需要什么)。

Get the first item from the list (if you can assume there's only one item with the class name): 从列表中获取第一项(如果您可以假设只有一个具有类名的项):

var newsTarget = document.getElementsByClassName('Scrolloutside');
newsTarget[0].style.top= "500px";

or iterate through the list (if there may be more than one item with that class name): 或遍历列表(如果可能有多个具有该类名称的项目):

var newsTarget = document.getElementsByClassName('Scrolloutside');
for (var i = 0; i < newsTarget.length; i++) {
    newsTarget[i].style.top= "500px";
}

Use as 用于

newsTarget[0].style.top= "500px";

The getElementsByClassName() method returns a collection of all elements in the document with the specified class name, as a NodeList object. getElementsByClassName()方法以NodeList对象的形式返回文档中具有指定类名的所有元素的集合。

The NodeList object represents a collection of nodes. NodeList对象代表节点的集合。 The nodes can be accessed by index numbers. 可以通过索引号访问节点。 The index starts at 0 索引从0开始

在页面底部使用它。

newsTarget[0].style.top= "500px";

暂无
暂无

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

相关问题 GetElementsByClassName(&#39;..&#39;)[0] .value =&#39;&#39;不起作用 - GetElementsByClassName('..')[0].value = '' does not work 为什么document.GetElementsByClassName在本地驱动器上有效,但在网络驱动器上无效? - Why does document.GetElementsByClassName work on local drive but not on a network drive? 为什么getElementsByClassName在Mozilla中不起作用? - Why getElementsByClassName do not work in Mozilla? 为什么getElementsByClassName()可以工作,而querySelector()不能工作? - Why would getElementsByClassName() work and not querySelector()? 为什么getElementsByClassName返回未定义? - Why does getElementsByClassName return undefined? 在 for_loop 中使用 getElementsByClassName 不起作用 - using getElementsByClassName in for_loop does not work proprly 为什么getElementsByClassName在xhr reponseXML上不起作用 - Why getElementsByClassName doesn't work on xhr reponseXML 使用 forEach 导航 HTML 元素时,getElementsByClassName 函数不适用于这些元素 - While navigating HTML Elements with forEach, the getElementsByClassName function does not work on these elements 当只有一个要选择的类时,为什么getElementsByClassName需要[0]? - Why is [0] needed for getElementsByClassName to work when there's only one class to select? 在getElementsByClassName的重新创建过程中,Array.prototype.slice.call如何工作? - How does Array.prototype.slice.call work in this recreation of getElementsByClassName?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM