Có thể thay đổi một hàm được gọi bởi sự kiện onmouseover hoặc onmouseout hiện có không? Đối với ví dụ sau là có một cách để tôi có ChangeItemAEvent thay đổi chức năng onmouseover "ItemA" từ ChangeColor() thành ChangeColorBack()? Hiện tại tôi cần khai báo một hàm hoàn toàn mới() mà tôi cảm thấy không thanh lịch vì tôi lặp lại mã khi tôi có thể gọi hàm hiện có.Tự động thay đổi onmouseover hoặc onmouseout để gọi một chức năng khác nhau
javascript:
function ChangeColor(elementid)
{
document.getElementById(elementid).style.background = "Orange";
document.getElementById(elementid).style.color = "Black";
}
function ChangeColorBack(elementid)
{
document.getElementById(elementid).style.background = "Black";
document.getElementById(elementid).style.color = "White";
}
function ChangeItemAEvent()
{
document.getElementById("ItemA").onmouseover = function() {
document.getElementById("ItemA").style.background = "Black";
document.getElementById("ItemA").style.color = "White";
};
}
html:
<span id="ItemA" onmouseover="ChangeColor(this.id)">
<button id="ButtonB" onclick="ChangeItemAEvent()">