简体   繁体   English

Js,在事件上将参数传递给函数

[英]Js, passing arguments to a function on event

I'm trying to pass arguments to a function i created, but i guess i'm missing something... Here's what i want to do: on event mouseover, the word and its translation get red. 我正在尝试将参数传递给我创建的函数,但是我想我缺少了一些东西……这就是我想要做的:在事件鼠标悬停时,单词及其翻译变红。 On event mouseout, these two words return black. 在事件mouseout上,这两个单词将返回黑色。 I started from this point - pure JavaScript (no function involved): 我从这一点开始-纯JavaScript(不涉及任何函数):

<span id ="e1" onmouseover="document.getElementById('c1').style.color='red'"
      onmouseout = "document.getElementById('c1').style.color='black'"> House </span>

I know i can use "this.style.color" but for my final target it's not useful... So, i used that approach to create a function. 我知道我可以使用“ this.style.color”,但对于我的最终目标却没有用……所以,我使用了这种方法来创建函数。 On event, arguments are passed to the function, and it colors the words, identifying them with the "getElementById". 事件发生时,参数将传递给函数,并为单词着色,并使用“ getElementById”进行标识。 I thought it was a good idea and that it would have been: 我认为这是个好主意,应该是:

<span id ="e1" onmouseover="color(e1,f1)" onmouseout="uncolor(e1,f1)"> House </span>    

<span id ="f1" onmouseover="color(e1,f1)" onmouseout="uncolor(e1,f1)">  Maison </span>

<!-- The script in js would be: -->

<script>
function color(e, f) {
  document.getElementById("e").style.color = "red";
  document.getElementById("f").style.color = "red";   
}
function uncolor(e, f) {
  document.getElementById("e").style.color = "black";    
  document.getElementById("f").style.color = "black";
}
</script>

I understand that it's a problem about passing arguments (i tried to pass a single argument to a function containing "alert(arg)" and it did not work). 我知道这是关于传递参数的问题(我试图将单个参数传递给包含“ alert(arg)”的函数,但它不起作用)。 I found something about the fact that the function receives the event itself as an argument, but i don't really know if it's the right direction or what... 我发现以下事实:该函数将事件本身作为参数接收,但是我真的不知道这是正确的方向还是什么。

Any hint will be appreciated, thanks. 任何提示将不胜感激,谢谢。

1st approach 第一种方法

You need to make 2 updates. 您需要进行2次更新。

Update html ( Add quotes around parameters ) 更新html( 在参数周围添加引号

<span id ="e1" onmouseover="color('e1','f1')" onmouseout="uncolor('e1','f1')"> House </span>    

<span id ="f1" onmouseover="color('e1','f1')" onmouseout="uncolor('e1','f1')">  Maison </span>

Update your script ( Remove quotes when using in function ) 更新您的脚本( 在函数中使用时删除引号

<script>
function color(e, f) {
  document.getElementById(e).style.color = "red";
  document.getElementById(f).style.color = "red";   
}
function uncolor(e, f) {
  document.getElementById(e).style.color = "black";    
  document.getElementById(f).style.color = "black";
}
</script>

2nd approach 第二种方法

No html update. 没有html更新。 Update your script to following 将您的脚本更新为以下内容

<script>
function color(e, f) {
  e.style.color = "red";
  f.style.color = "red";   
}
function uncolor(e, f) {
  e.style.color = "black";    
  f.style.color = "black";
}
</script>

By using (this) it's more simple . 通过使用(this)更简单。

Updated Code 更新的代码

<span id ="e1" onmouseover="color(this)" onmouseout="uncolor(this)"> House </span>

<span id ="f1" onmouseover="color(this)" onmouseout="uncolor(this)">  Maison </span>

By Using "this" you pass the full control to function. 通过使用“ this”,您可以将全部控件传递给功能。

  function color(control) {
  control.style.color = "red";
  control.style.color = "red";   
  }

 function uncolor(control) {
  control.style.color = "black";    
  control.style.color = "black";
 }

Perhaps I'm missing the point, but this should all be done in CSS if at all possible. 也许我没有抓住重点,但是如果可能的话,所有这些都应该在CSS中完成。 You can use the adjacent sibling selector + or general sibling selector ~ to get the translation that appears after the word. 您可以使用相邻的同级选择器+或常规同级选择器~来获取单词后出现的翻译。 However, there's no widely supported way to go from the translation to a previous sibling. 但是,从翻译到先前的兄弟姐妹没有广泛支持的方法。 The easiest way to overcome that is to put both elements inside another container. 解决该问题的最简单方法是将两个元素都放在另一个容器中。

Simplified HTML: 简化的HTML:

<div class="word-pair">
    <span id ="e1" class="word"> House </span>    
    <span id ="f1" class="translation">  Maison </span>
</div>

CSS: CSS:

div.word-pair span{
    color:#000;
    transition:all .2s ease-in-out;
}
div.word-pair:hover span {
    color:red;
}

Fiddle Example: http://jsfiddle.net/kw7wyux4/ 小提琴示例: http : //jsfiddle.net/kw7wyux4/

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

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