我是Js的小伙子,我刚开始学习。但我没有在函数中使用该名称。我在任何教程中都没有看到相关描述,所以我来找你!对此类问题感到抱歉。例如:
var hexagon = function (hover/THIS POINT) {
var hex1 = document.getElementById('hexagon1_work');
hex1.addEventListener('mouseover', function(){
document.getElementsByClassName('content')[0]
.style.backgroundColor = '#000000';
});
hex1.addEventListener('mouseout', function(){
document.getElementsByClassName('content')[0]
.style.backgroundColor = 'transparent';
});
var hex2 = document.getElementById('hexagon2_work');
hex2.addEventListener('mouseover', function(){
document.getElementsByClassName('content')[0]
.style.backgroundColor = '#EAE080';
});
hex2.addEventListener('mouseout', function(){
document.getElementsByClassName('content')[0]
.style.backgroundColor = 'transparent';
});
那会对我有所帮助。 还有其他方法来编写函数吗?例如:
function() { ... }
谢谢。 :) 麦克
答案 0 :(得分:1)
在Javascript中编写函数有多种方法。
命名(非匿名)函数的示例:
function myFunction(a, b) {
return a * b;
}
myFunction是函数的名称,括号中的东西是参数。它们很有用,因为它们可以在不重复代码的情况下对不同的值执行相同的操作。
匿名函数:
var myFunction = function(a,b) {
return a * b;
};
在这种情况下,myFunction不是函数的名称,而是一个具有匿名函数作为其值的变量。
通过调用constuctor创建函数:
var myFunction = new Function("a","b","return a * b;");
以上所有内容都是相同的,您可以将所有内容称为:
var x = myFunction(4, 3); //result is 12
var x = myFunction(2, 5); //result is 10
更多信息(初学者易于理解):w3schools tutorial,wikibooks