简体   繁体   English

使用JavaScript,onmouseover和onmouseout更改Div内容?

[英]Change Div Content with JavaScript, onmouseover and onmouseout?

I have the following code: 我有以下代码:

window.onload = function createDivs() { 
    for(var i = 1;i<29;i++) {
        var div = document.createElement("div"); 
        var body = document.getElementsByTagName("body")[0];
        var n1 = document.createTextNode("Cell " + i);
        var n2 = document.createTextNode(i + " Cell");
        div.style.width = "100px";
        div.style.height  = "100px";
        div.style.border = "1px solid red";
        div.style.cssFloat = "left";
        div.style.margin = "1px"
        div.className = i;
        body.appendChild(div);
    }
    div.onmouseover = function() {
        this.appendChild(n1);
    },
    div.onmouseout = function() {
        this.appendChild(n2);
    } 
}

what I want to acheive 我要达到的目标

  1. on mouseover of each div, the div should have a text of cell 1, cell 2, ..... upto cel 28. But I am just getting Cell 28 on hover for each cell. 在每个div的mouseover上,div应当具有单元格1,单元格2,.........最高cel 28的文本。但是我只是将每个单元格的单元格28悬停了。

2. I also want to achieve that onmouseout, the cell should have " 1 cell " as text, but its not working. 2.我也想实现onmouseout,该单元格应该以“ 1 cell ”作为文本,但是它不起作用。

Any help is appreciated. 任何帮助表示赞赏。

http://jsbin.com/iXuLEDE/7/edit?html,output http://jsbin.com/iXuLEDE/7/edit?html,output

Your problem is coming from your closure over n1 and n2 . 您的问题来自对n1n2封闭。 The simplest solution to that is the following. 下面是最简单的解决方案。

From this: 由此:

div.onmouseover = function() {
    this.appendChild(n1);
}

To this: 对此:

div.onmouseover = (function(text) {
      return function () {
          this.innerHTML = text;
      }
}(n1.textContent));

This way you are using a copy of the text node (by using it as a parameter to a function) rather than as a closure later on. 这样,您将使用文本节点的副本(通过将其用作函数的参数)而不是稍后的闭包。

UPDATE UPDATE

Just read the second part of your question, this should work: 只需阅读问题的第二部分,这应该可以工作:

div.onmouseover = (function(text) {
    return function() {
        this.innerHTML = text;
    };
}("Cell " + i));
div.onmouseout = (function(text) {
    return function() {
        this.innerHTML = text;
    };
}(i + " Cell"));

USING TEXT NODES 使用文字节点

function createDivs() { 
    for(var i = 1;i<29;i++) {
        var div = document.createElement("div"); 
        var body = document.getElementsByTagName("body")[0];
        div.style.width = "100px";
        div.style.height  = "100px";
        div.style.border = "1px solid red";
        div.style.cssFloat = "left";
        div.style.margin = "1px"
        div.className = i;
        var n1 = document.createTextNode("Cell " + i);
        var n2 = document.createTextNode(i + " Cell");
        body.appendChild(div);
        div.onmouseover = (function(n_text1, n_text2) {
          return function() {
              if (n_text2.parentNode == this) {
                  this.removeChild(n_text2);
              }
              this.appendChild(n_text1);
          };
        }(n1, n2));
        div.onmouseout = (function(n_text1, n_text2) {
          return function() {
              if (n_text1.parentNode == this) {
                  this.removeChild(n_text1);
              }
              this.appendChild(n_text2);
          };
        }(n1, n2));
    }
}

Fiddle here: http://jsfiddle.net/Mk5e5/ 在这里提琴: http : //jsfiddle.net/Mk5e5/

Please change the code like this 请像这样更改代码

    window.onload = function createDivs() { 
    for(var i = 1;i<29;i++) {
        var div = document.createElement("div"); 
        div.setAttribute("index", i);
        var body = document.getElementsByTagName("body")[0];
        div.style.width = "100px";
        div.style.height  = "100px";
        div.style.border = "1px solid red";
        div.style.cssFloat = "left";
        div.style.margin = "1px"
        div.className = i;
        body.appendChild(div);
        div.onmouseover = function() {
          var n1 = document.createTextNode("Cell " + this.getAttribute("index"));
          this.appendChild(n1);

        } ,
        div.onmouseout = function() {
        var n2 = document.createTextNode(this.getAttribute("index") + " Cell");
          this.appendChild(n2);

        } 

    }
}

You should add event for each div in loop 您应该为循环中的每个div添加事件

Try to understand javascript closures, specially inside for loops. 尝试了解javascript闭包,特别是for循环内部。 Check this excellent explanation in this blog post: http://www.mennovanslooten.nl/blog/post/62 在此博客文章中查看以下出色的解释: http : //www.mennovanslooten.nl/blog/post/62

Change your createDivs function to: 将您的createDivs函数更改为:

function createDivs() { 

for(var i = 1;i<29;i++) {
    var div = document.createElement("div"); 
    var body = document.getElementsByTagName("body")[0];
    div.style.width = "100px";
    div.style.height  = "100px";
    div.style.border = "1px solid red";
    div.style.cssFloat = "left";
    div.style.margin = "1px"
    div.className = i;
    body.appendChild(div);
    div.onmouseover = (function(value) { 
        return function() {
            var n1 = document.createTextNode("Cell " + value);
            this.appendChild(n1);
        }
    })(i);
    div.onmouseout = (function(value) { 
        return function() {
            var n2 = document.createTextNode(value + " Cell");
            this.appendChild(n2);
        }
    })(i);

};

}; };

Your code doesn't work because when your onmouseover and onmouseout functions are executed, the value of your text variables is "cell 28." 您的代码不起作用,因为执行onmouseoveronmouseout函数时,文本变量的值为“单元格28”。 Also, your child removal was a little off, if I interpret your intentions correctly. 此外,如果我正确地解释了您的意图,您的孩子离去也有些困难。

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  <script type="text/javascript">
    function createDivs(n) { 
      for(var i = 1; i <= n; i++) {
        var div = document.createElement("div"); 
        var body = document.getElementsByTagName("body")[0];

        div.style.width = "100px";
        div.style.height  = "100px";
        div.style.border = "1px solid red";
        div.style.cssFloat = "left";
        div.style.margin = "1px"
        div.className = i;
        body.appendChild(div);

        div.onmouseover = function() {
          if (this.childNodes.length > 0) this.removeChild(this.childNodes[0]);
          var n_text = document.createTextNode("Cell " + this.className);
          this.appendChild(n_text);
        },
        div.onmouseout = function() {
          if (this.childNodes.length > 0) this.removeChild(this.childNodes[0]);
          var n_text = document.createTextNode(this.className + " Cell");
          this.appendChild(n_text);
        }  
      }   
    }

    createDivs(28)

  </script>

</body>
</html>

I also changed your if statement so you pass the number of cells you want instead of the number of cells +1. 我还更改了if语句,以便您传递所需的单元数而不是单元数+1。

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

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