How to reduce the use of onmouseover and onmouseoutt?

My page contains numerous sections that I want to highlight when the mouse moves. This can be done with onmouseoverand onmouseout. I have more than 100 sections, and I don’t think it is very efficient to call methods in each section. In this way.

<li id="1" onmouseover="h(1)" onmouseout="h(1)">
    <label>1</label>
</li>
<li id="2" onmouseover="h(2)" onmouseout="h(2)">
    <label>2</label>
</li>
<li id="3" onmouseover="h(3)" onmouseout="h(3)">
    do something
</li>
...
<li id="4" onmouseover="h(4)" onmouseout="h(4)">
    do something
</li>

I hope to get this answer in Javascript since I don't know jQuery.

+3
source share
5 answers

What you need is event delegation . This binds the event handler to a common ancestor. In your case, it could be something like strings:

// assuming `ul` refers to the list element that contains those `li` elements
ul.onmouseover = function(event) {
    // some cross-browser handling (IE)
    event = event || window.event;
    var target = event.target || event.srcElement;

    // if the event was triggered on a `li` element
    if(target.nodeName === 'LI') {
        h(this.id);  // do the stuff
    }
};

Demo

, , . li , .

quirksmode.org, .


, jQuery , , mouseenter mouseleave IE, .

:

$('#listId').on('mouseenter', 'li', function() {
    h(this.id);
});

$('#listId').on('mouseleave', 'li', function() {
    h(this.id);
});

/ DOM-, jQuery .

+6
<li id="1" class="btn">
do something
</li>

JS:

<script type="text/javascript">
    window.onload = function(){
       var allBtns = document.getElementsByClassName("btn");
       for (var i = 0; i < allBtns.length; i++) {
         allBtns[i].onmouseover = function(){h(this.id)};
         allBtns[i].onmouseout = function(){h(this.id)};
        }
     }
</script>

<ul class="myBTNS">
   <li id="1">do something</li>
   <li id="2">do something</li>
<ul>

JS:

<script type="text/javascript">
    window.onload = function() {
        var btnsContainer = document.getElementsByClassName("myBTNS");
        for (var i = 0; i < btnsContainer.length; i++) {
            var allBtns = btnsContainer[i].getElementsByTagName("li");
            for (var i = 0; i < allBtns.length; i++) {
                allBtns[i].onmouseover = function() {h(this.id)};
                allBtns[i].onmouseout = function() {h(this.id)};
            }
        }
    }
</script>

+1

:

HTML: <li>, , /, . 'someclass', , . , . <li class="someclass anotherclass"></li>.

<ul>
  <li class="someclass secondaryclass">Example</li>
  <li class="someclass">Example</li>
  <li class="someclass">Example</li>
  <li class="someclass">Example</li>
<ul>

JavaScript: "someclass" . out 'over', jsFiddle, .

for(var i in document.getElementsByClassName('someclass'))
{
    document.getElementsByClassName('someclass')[i].addEventListener('mouseover', over ,false);
    document.getElementsByClassName('someclass')[i].addEventListener('mouseout', out ,false);   
}

http://jsfiddle.net/eVs9L/ < - , .

jQuery:

$('.someclass').hover(function(){
  //Mouse Over Code goes here...
}, function(){
  //Mouse Out code goes here...
});
+1
0

document.getElementsByTagName, li . onmouseover onmouseout.

var elems = document.getElementsByTagName('li');
for (var i = 0; i < elems.length; i++) {
    elems[i].onmouseover = function () {
        this.innerHTML = "over";
        this.style.background = "yellow";
    };
    elems[i].onmouseout = function () {
        this.innerHTML = "out";
        this.style.background =  "white";
    };
}

Here is a link to a demo in jsFiddle. I made a function to change the text and color of bg to demonstrate that the code is really doing something.

0
source

All Articles