如何使用事件对象来显示有关DOM元素的信息

时间:2013-01-18 18:28:58

标签: javascript events alert

我希望能够在网页中点击一个框(通过代码创建框,并从表单接收值)并显示有关该框的信息。我正在使用display()函数,该函数使用事件对象和警报来显示有关该框的信息。到目前为止,我尝试执行此操作时遇到了多次奇怪的失败,这使我相信我没有正确访问对象属性。我是初学者,所以这可能非常明显,但感谢你的帮助。

构造函数:

function Box (counter, name, color, number, coordinates) {  
    this.counter = counter;
    this.name = name;
    this.color = color;
    this.number = number;
    this.coordinates = coordinates; 
}

全局变量:

var boxes = []; 
var counter = 0;

初始化功能:

function init() {     
    var generateButton = document.getElementById("generateButton");
    generateButton.onclick = getBoxValues;

    var clearButton = document.getElementById("clearButton");
    clearButton.onclick = clear;
}

从表单中获取值的函数:

function getBoxValues() {
    var nameInput = document.getElementById("name");  
    var name = nameInput.value; 

    var numbersArray = dataForm.elements.amount;
    for (var i = 0; i < numbersArray.length; i++) {
        if (numbersArray[i].checked) {
            number = numbersArray[i].value;
        }
    }  

    var colorSelect = document.getElementById("color");  
    var colorOption = colorSelect.options[colorSelect.selectedIndex];  
    var color = colorOption.value;                       

    if (name == null || name == "") {                    
        alert("Please enter a name for your box");
        return;
    } else {
        var newbox = new Box(counter, name, color, number, "coordinates");  
        boxes.push(newbox);
        counter++;
        /*for(m = 0; m < boxes.length; m++) {
            counter.newbox = boxes[m];
        }*/
    }

    addBox(newbox);

    var data = document.getElementById("dataForm");               
    data.reset();
}

为框分配属性的函数:

function addBox(newbox) {  
    for (var i = 0; i < newbox.number; i++) {                            
        var scene = document.getElementById("scene");              
        var div = document.createElement("div");                  
        div.className += " " + "box"; 
        div.innerHTML += newbox.name; 
        div.style.backgroundColor = newbox.color; 
        var x = Math.floor(Math.random() * (scene.offsetWidth-101));
        var y = Math.floor(Math.random() * (scene.offsetHeight-101));
        div.style.left = x + "px";
        div.style.top = y + "px"; 
        scene.appendChild(div); 
        div.onclick = display; 
        //console.log(newbox); 
        //shows all of the property values of newbox in the console
        //console.log(div); shows that it is an object in the console  
        //console.log(div.hasAttribute(number));  says false               
    }                  
}

显示功能:

function display(e) {
    // alert(e.target);  says its an html object
    //alert(e.target.className); works - says "box"
    //alert(e.target.hasAttribute(name)); says false
}

我已经在评论中找到了一些我发现的东西。

1 个答案:

答案 0 :(得分:0)

事件对象只为您提供名称而不是对元素的引用。所以......有几件事。

首先,如果你想与浏览器无关,你需要类似的东西(e.srcElement用于IE):

    var x = e.target||e.srcElement;

然后获取元素的引用并执行您想要的操作:

    var refToElement = document.getElementById(x.id);