Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaScript中编写鼠标按下事件?_Javascript_Event Handling_Onmousedown - Fatal编程技术网

如何在JavaScript中编写鼠标按下事件?

如何在JavaScript中编写鼠标按下事件?,javascript,event-handling,onmousedown,Javascript,Event Handling,Onmousedown,这是非常基本的,我相信JavaScript,但我有一个困难的时间,所以任何帮助将不胜感激 我想使用对象的第二个子节点中发生的mouseDown事件调用for循环中的函数。斜体部分是我的尝试。顺便说一下,swapFE功能仍在进行中。还有一件事是,当我把斜体部分放在swapFE函数中时,一切都正常工作,但当我把它放在for循环中时,它不会全部显示出来。我不知道为什么。当我用鼠标点击法语短语时,我基本上是想把它换成英语短语 function setUpTranslation() { var ph

这是非常基本的,我相信JavaScript,但我有一个困难的时间,所以任何帮助将不胜感激

我想使用对象的第二个子节点中发生的mouseDown事件调用for循环中的函数。斜体部分是我的尝试。顺便说一下,swapFE功能仍在进行中。还有一件事是,当我把斜体部分放在swapFE函数中时,一切都正常工作,但当我把它放在for循环中时,它不会全部显示出来。我不知道为什么。当我用鼠标点击法语短语时,我基本上是想把它换成英语短语

function setUpTranslation() {
   var phrases = document.getElementsByTagName("p");
   var swapFE = document.getElementsByTagName("phrase");

   for (i = 0; i<phrases.length; i++) {
      phrases[i].number = i;
      phrases[i].childNodes[1].innerHTML = french[i];

      *phrases[i].childNodes[1].onMouseDown = swapFE;*

      }
  }


    /* see "function_swapFE(phrase,phrasenum);" below. The expression to call function swapFE
    is located underneath "function swapFE(e)" because although the directions said to put the
    "run swapFE" within the for loop it did not work properly that's why I put it beneath the 
    "function swapFE(e)".*/



function swapFE(e) {
    var phrase = eventSource(e);
    var phasenum = parseInt(1) = [1].innercontent.previousSibling;

    phrase.node.previousSibling.onmousedown=swapFE
    function_swapFE(e)(phrase,phrasenum);
   }
函数设置翻译(){
var短语=document.getElementsByTagName(“p”);
var swapFE=document.getElementsByTagName(“短语”);

对于(i=0;i,您将创建一个名为swapFE的局部变量

var swapFE= document.getElementsByTagName(“短语”)

然后,您将此变量设置为mouseDown

短语[i].childNodes[1].onMouseDown= swapFE*


这是不对的…onMouseDown应该设置为函数名,而不是该名称的局部变量。因此,您可能应该将局部变量重命名为其他名称。这至少会让您更接近解决方案。

我只能猜测一下源代码可能出现的问题。首先,以下代码假设ll
标记至少有两个子元素:

for (i = 0; i<phrases.length; i++) { 
    phrases[i].number = i; 
    phrases[i].childNodes[1].innerHTML = french[i]; 

    *phrases[i].childNodes[1].onMouseDown = swapFE;* 
} 
将局部重写该函数的全局函数
swapFE
,并替换为变量

以下是我编写
setupTranslation
函数的方法:

function setUpTranslation() {     
    var phrases = document.getElementsByTagName("p");
    // rename the swapFE var as outlined below
    var swapFENodes = document.getElementsByTagName("phrase");

    var cNode;  // set up an empty variable that we use inside the loop
    for (i = 0; i<phrases.length; i++) {
        /* Check for the existence of the translationPhrase class 
           in the <p> tag and the set the cNode var to childNodes[1]
           and testing for its existence at the same time */
        if (cNode.className != "translationPhrase" 
             || !(cNode = phrases[i].childNodes[1]))
             continue; // skip to the next iteration

         phrases[i].number = i;
         cNode.innerHTML = french[i];     
         cNode.onmousedown = swapFE;  // Changed onMouseDown to onmousedown
    }     
}    
函数setUpTranslation(){
var短语=document.getElementsByTagName(“p”);
//重命名swapFE变量,如下所述
var swapFENodes=document.getElementsByTagName(“短语”);
var cNode;//设置一个在循环中使用的空变量

对于(i=0;i@Ashley,您不能将斜体或其他代码格式应用于此处的代码块,所以。我自己也曾陷入过这样的陷阱:-)我明白您的意思。我实际上想做什么(可能还不太清楚)在onMouseDown部分中引用了我正在处理的swapFE函数。所以我删除了var swapFE,现在我将它引用到for循环下面的swapFE函数。谢谢你的帮助Andy E。我知道你对我的代码说了什么,假设我至少有2个子元素,但我想做的是s指的是对象的第二个子节点(这就是为什么我放[1]而不是[0]。你知道如何修复它吗?@Ashley:你可以用
if
语句检查节点是否存在。我已经更新了我的答案,在底部我可以如何编写你的函数。
function setUpTranslation() {     
    var phrases = document.getElementsByTagName("p");
    // rename the swapFE var as outlined below
    var swapFENodes = document.getElementsByTagName("phrase");

    var cNode;  // set up an empty variable that we use inside the loop
    for (i = 0; i<phrases.length; i++) {
        /* Check for the existence of the translationPhrase class 
           in the <p> tag and the set the cNode var to childNodes[1]
           and testing for its existence at the same time */
        if (cNode.className != "translationPhrase" 
             || !(cNode = phrases[i].childNodes[1]))
             continue; // skip to the next iteration

         phrases[i].number = i;
         cNode.innerHTML = french[i];     
         cNode.onmousedown = swapFE;  // Changed onMouseDown to onmousedown
    }     
}