【问题标题】:getElementByClassName does not work where getElementById does (browser supported)getElementsByClassName 在 getElementById 起作用的地方不起作用(支持浏览器)
【发布时间】:2015-02-26 03:30:39
【问题描述】:

有效的脚本:

function getEventTarget(e) {
    e = e || window.event;
    return e.target || e.srcElement; 
}
var ul = document.getElementById('commandList');
ul.onclick = function(event) {
    var target = $(getEventTarget(event)).clone().children().remove().end().text();
    document.getElementById('inputCommand').value = target;
};

var interface = '/cgi-bin/CGIProxy.fcgi?cmd=';
var protocol = 'http://';
var host, command, access, commandUrl;

$('.sendCommand').click(function() {
    host = $('#cameraLocation').val();
    access = $('#cameraAccess').val();
    command = $('#inputCommand').val() + $('#inputParam').val();
    commandUrl = protocol + host + interface + command + access;                
    $('#showUrl').html(commandUrl);
    $('#showResult').attr('src', commandUrl);
});

当我将document.getElementById 更改为document.getElementsByClassName(相关元素同时具有IDClass 作为"commandList")时,脚本不再工作,我错过了什么?谢谢!

新脚本:

function getEventTarget(e) {
    e = e || window.event;
    return e.target || e.srcElement; 
    }
var ul = document.getElementsByClassName('commandList');
ul.onclick = function(event) {
    var target = $(getEventTarget(event)).clone().children().remove().end().text();
    document.getElementById('inputCommand').value = target;
    };

var interface = '/cgi-bin/CGIProxy.fcgi?cmd=';
var protocol = 'http://';
var host, command, access, commandUrl;

$('.sendCommand').click(function() {
    host = $('#cameraLocation').val();
    access = $('#cameraAccess').val();
    command = $('#inputCommand').val() + $('#inputParam').val();
    commandUrl = protocol + host + interface + command + access;                
    $('#showUrl').html(commandUrl);
    $('#showResult').attr('src', commandUrl);
});

【问题讨论】:

    标签: javascript jquery dom javascript-events event-handling


    【解决方案1】:

    getElementsByClassName() 返回匹配元素的 HTMLCollection,因为多个元素可以共享一个公共类属性。

    您必须遍历返回的元素并对您的代码进行相关修改以进行调整。

    见:https://developer.mozilla.org/en-US/docs/Web/API/document.getElementsByClassName

    【讨论】:

      【解决方案2】:

      getElementsByClassName() 返回元素的 NodeList,而 getElementById()(和 querySelector())返回单个节点。

      要对类似数组的 NodeList 进行操作,您必须遍历 NodeList 中包含的每个元素,并分别为每个元素指定操作;例如:

      Array.prototype.forEach.call(document.getElementsByClassName('whatever'), function (elem) {
          elem.onclick = function () {
              // do stuff;
          }
      });
      

      或者:

      Array.prototype.forEach.call(document.getElementsByClassName('whatever'), function (elem) {
          elem.addEventListener('click', functionToCallOnClick);
      });
      

      【讨论】:

      • 我仍然无法让这个工作,你介意看看:goo.gl/LD4JxV - 点击任何命令(列表元素)将填充“inputCommand”字段。现在它正在使用 document.getElementById 并且我正在尝试切换到 getElementByClassName 以便我也可以收听其他元素。非常感谢!
      • 同时发现问题是class="list-unstyled" class="commandList"而不是class="list-unstyled commandList",浏览器没有报错但是JS引擎不会识别错误。感谢您的时间和回答!
      猜你喜欢
      • 1970-01-01
      • 2018-06-11
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2012-05-02
      • 2019-08-11
      • 1970-01-01
      • 2014-04-22
      相关资源
      最近更新 更多