简体   繁体   English

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

[英]how to use an event object to dispaly information about a DOM element

I want to be able to click on a box (the boxes are created through code, and receive values from a form) in the webpage and display information about the box. 我希望能够在网页中单击一个框(框是通过代码创建的,并从表单接收值)并显示有关该框的信息。 I am working on a display() function that uses an event object and an alert to display information about the box. 我正在使用一个事件对象和一个警报来显示有关该框的信息的display()函数。 So far, I've had multiple odd failures in my attempt to do this, which leads me to believe that I'm not accessing object attributes correctly. 到目前为止,我在尝试执行此操作时遇到了许多奇怪的失败,这使我相信我没有正确访问对象属性。 I'm a beginner, so this could be really obvious, but thanks for the help. 我是一个初学者,所以这可能确实很明显,但是感谢您的帮助。

constructor function: 构造函数:

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

Global variables: 全局变量:

var boxes = []; 
var counter = 0;

Init function: 初始化功能:

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

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

Function that gets values from the form: 从表单获取值的函数:

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 that assigns attributes to the boxes: 将属性分配给框的函数:

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               
    }                  
}

display function: 显示功能:

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
}

I've included some of the things i've found in comments. 我已经在评论中找到了一些发现。

The event object only gives you the name not a reference to the element. 事件对象仅为您提供名称,而不是对元素的引用。 So... a couple of things. 所以...几件事。

First if you want to be browser agnostic you want something like (e.srcElement is for IE): 首先,如果您想与浏览器无关,则需要类似的东西(e.srcElement用于IE):

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

Then get a reference to the element and do what you want: 然后获取对该元素的引用并执行所需操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM