的作品的脚本:getElementByClassName的地方不工作的getElementById没有(支持的浏览器)
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
(有问题的元素既有ID
和Class
为"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);
});
我仍然无法得到这个工作,你会介意看看:http://goo.gl/LD4JxV - 点击任何命令(名单元素)将填充“inputCommand”字段。现在它正在工作,使用document.getElementById,我试图切换到getElementByClassName,所以我也可以听其他元素。非常感谢! – Yatko
同时,发现问题'class =“list-unstyled”class =“commandList”'而不是'class =“list-unstyled commandList”',浏览器没有提供任何错误,但JS引擎不会理解错误。感谢您的时间和答案! – Yatko