简体   繁体   English

分配对象的参数值javascript

[英]assign parameter value of an object javascript

I have been looking at this code for a long time trying to figure this out, but I am having no luck. 我一直在看这段代码,试图弄清楚这一点,但是我没有运气。 This issue is that I want to assign a value to the parameter boxId. 这个问题是我想给参数boxId赋一个值。 When I click on a box in the webpage an alert will come up displaying that id. 当我单击网页上的一个框时,将显示一个警报,显示该ID。 I have tried many things, but nothing seems to work. 我尝试了很多事情,但似乎没有任何效果。 I'm a beginner, so I feel at this point there just must be something that I don't know how to do. 我是一个初学者,所以我觉得在这一点上肯定有些事情我不知道该怎么做。

constructor function: 构造函数:

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

global variables: 全局变量:

  var boxes = []; 
  var counter = 0; 
  var boxId = 0;

init function: 初始化函数:

  window.onload = init;

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

function to get values and create new boxes: 函数获取值并创建新框:

  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(boxId, name, color, number, "coordinates");  
    boxes.push(newbox);
    counter++;
    var boxId = counter;
    }

    addBox(newbox);

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

function that adds boxes to the page: 将框添加到页面的功能:

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

function to display alert when box is clicked: 单击框时显示警报的功能:

  function display(e) {
  var a = e.target;
  alert(a.counter);
  }

function to clear boxes: 清除框的功能:

function clear() {
  var elems = document.getElementsByClassName("box");
  for ( k = elems.length - 1; k >= 0; k--) {
   var parent = elems[k].parentNode;
   parent.removeChild(elems[k]);
  }
  }

All of the other functions work just fine. 所有其他功能都可以正常工作。 I keep running into the id showing up as "undefined" when I click it, or the counter displaying "0" in the console log, for everything I've tried. 对于我尝试过的所有操作,单击ID或在控制台日志中的计数器显示为“ 0”时,我都会不断遇到显示为“ undefined”的ID。

You can do it like this. 您可以这样做。

First, in addBox() embed boxId as an tag's attribute like this: 首先,在addBox()中将boxId嵌入为标签的属性,如下所示:

div.setAttribute('data-boxId', newbox.boxId);

Then in display() you can retrieve it back: 然后在display()中可以将其取回:

alert(e.target.getAttribute('data-boxId'));

Please tell if you do not prefer this approach and I will post an alternative (closure things). 请告知您是否不喜欢这种方法,我将发布替代方法(关闭事项)。

Edit: Add jsfiddle example http://jsfiddle.net/runtarm/8FJpU/ 编辑:添加jsfiddle示例http://jsfiddle.net/runtarm/8FJpU/

One more try. 再试一次。 Perhaps if you change: 也许如果您更改:

var boxId = counter;

to

boxId = counter;

It will then use the boxId from the outer scope instead of the one defined in the function getBoxValues() 然后,它将使用外部范围中的boxId而不是函数getBoxValues()定义的getBoxValues()

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

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