我有点困惑的是,当点击任何hero.attacks
时,为什么此函数始终将li.attack
设置为hero.atatcks
- 数组的第一个元素?我正在尝试将其设置为相应的var hero = {
attacks: [{
name: "atck1"
}, {
name: "atck2"
}, {
name: "atck3"
}, {
name: "atck4"
}]
};
var attacks = document.getElementsByClassName("attack");
for (var i = 0; i < attacks.length; i++) {
var atck = hero.attacks[i];
attacks[i].addEventListener("click", function (event) {
document.getElementById("attack").innerText = atck.name;
})
}
- 元素的名称。我已尝试使用for循环和foreach以及循环之外的索引集,每次迭代都会增加,但似乎没有任何效果?
http://jsfiddle.net/47r768p8/2/
使用Javascript:
<ul>
<li class="attack">Atck1</li>
<li class="attack">Atck2</li>
<li class="attack">Atck3</li>
<li class="attack">Atck4</li>
</ul>
<h1 id="attack"></h1>
HTML:
using UnityEngine;
using System.Collections;
public class FormationController : MonoBehaviour
{ //Spawning Variables
public GameObject EnemyPrefab;
public float W = 10, H = 5;
//Movement Variables
public float Speed = 5;
private int Direction;
private float BoundaryRightEdge, BoundaryLeftEdge;
public float Padding = 0.25f;
void Start() //Setting Boundary
{
Camera camera = GameObject.Find("Player").GetComponentInChildren<Camera>();
float distance = camera.transform.position.z - camera.transform.position.z;
BoundaryLeftEdge = camera.ViewportToWorldPoint(new Vector3(0, 0, distance)).x + Padding;
BoundaryRightEdge = camera.ViewportToWorldPoint(new Vector3(1, 1, distance)).x - Padding;
}
void OnDrawGizmos()
{
float xmin, xmax, ymin, ymax;
xmin = transform.position.x - 0.5f * W;
xmax = transform.position.x + 0.5f * W;
ymin = transform.position.y - 0.5f * H;
ymax = transform.position.y + 0.5f * H;
Gizmos.DrawLine(new Vector3(xmin, ymin, 0), new Vector3(xmin, ymax));
Gizmos.DrawLine(new Vector3(xmin, ymax, 0), new Vector3(xmax, ymax));
Gizmos.DrawLine(new Vector3(xmax, ymax, 0), new Vector3(xmax, ymin));
Gizmos.DrawLine(new Vector3(xmax, ymin, 0), new Vector3(xmin, ymin));
}
void Update()
{
float formationRightEdge = transform.position.x + 0.5f * W;
float formationLeftEdge = transform.position.x - 0.5f * W;
if (formationRightEdge > BoundaryRightEdge)
{
Direction = -1;
}
if (formationLeftEdge < BoundaryLeftEdge)
{
Direction = 1;
}
transform.position += new Vector3(Direction * Speed * Time.deltaTime, 0, 0);
//Spawn Enemies on Keypress
if (Input.GetKeyDown(KeyCode.S))
{
foreach (Transform child in transform)
{
GameObject enemy = Instantiate(EnemyPrefab, child.transform.position, Quaternion.identity) as GameObject;
enemy.transform.parent = child;
}
}
}
答案 0 :(得分:0)
您的数组// Production steps of ECMA-262, Edition 5, 15.4.4.19
// Reference: http://es5.github.io/#x15.4.4.19
if (!Array.prototype.map) {
Array.prototype.map = function(callback, thisArg) {
var T, A, k;
if (this == null) {
throw new TypeError(' this is null or not defined');
}
// 1. Let O be the result of calling ToObject passing the |this|
// value as the argument.
var O = Object(this);
// 2. Let lenValue be the result of calling the Get internal
// method of O with the argument "length".
// 3. Let len be ToUint32(lenValue).
var len = O.length >>> 0;
// 4. If IsCallable(callback) is false, throw a TypeError exception.
// See: http://es5.github.com/#x9.11
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function');
}
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
if (arguments.length > 1) {
T = thisArg;
}
// 6. Let A be a new array created as if by the expression new Array(len)
// where Array is the standard built-in constructor with that name and
// len is the value of len.
A = new Array(len);
// 7. Let k be 0
k = 0;
// 8. Repeat, while k < len
while (k < len) {
var kValue, mappedValue;
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the HasProperty internal
// method of O with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
if (k in O) {
// i. Let kValue be the result of calling the Get internal
// method of O with argument Pk.
kValue = O[k];
// ii. Let mappedValue be the result of calling the Call internal
// method of callback with T as the this value and argument
// list containing kValue, k, and O.
mappedValue = callback.call(T, kValue, k, O);
// iii. Call the DefineOwnProperty internal method of A with arguments
// Pk, Property Descriptor
// { Value: mappedValue,
// Writable: true,
// Enumerable: true,
// Configurable: true },
// and false.
// In browsers that support Object.defineProperty, use the following:
// Object.defineProperty(A, k, {
// value: mappedValue,
// writable: true,
// enumerable: true,
// configurable: true
// });
// For best browser support, use the following:
A[k] = mappedValue;
}
// d. Increase k by 1.
k++;
}
// 9. return A
return A;
};
}
由相同的项组成,重复多次,这就是为什么它似乎每次都设置相同的项目。请尝试以下方法:
hero.attacks