【发布时间】: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(相关元素同时具有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);
});
【问题讨论】:
标签: javascript jquery dom javascript-events event-handling