1

I'm trying to call a function defined in an external .js using jQuery, but nothing happens. Here is my html:

<head>
    <title>Página de pruebas jQuery</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <!--<link rel="StyleSheet" href="css/style.css" type="text/css" />-->
    <script type="text/javascript" src="jquery-1.10.2.min.js" /></script>
    <script type="text/javascript" src="E1.js" />
    </script>
</head>

<body>
    <div id="contenedor">
        Pasa el ratón por aquí
    </div>

    <div id="mostrador" style="display: none;">
        Muy bien, has pasado el raton por encima!
    </div>
</body>

And my jQuery code:

$(document).ready(function(){
    $("#contenedor").mouseenter(mostrarTexto(evento));

    $("#contenedor").mouseleave(ocultarTexto(evento));
});


    var mostrarTexto=function(evento){
        $("#mostrador").css("display","block");
    }

    var ocultarTexto=function(evento){
        $("#mostrador").css("display","none");
    }

I've tried a lot of things I've searched, but i can't get it to work

Francesco Callari
  • 11,300
  • 2
  • 25
  • 40
Alex
  • 750
  • 5
  • 15
  • 33
  • first check whether jquery is in correct path..try to put alert inside document.ready(function()) – sree Jan 23 '14 at 12:54
  • Make sure the script files(jQuery library and E1) are properly imported in your page. – kpmDev Jan 23 '14 at 13:14

4 Answers4

3

Make sure the script files(jQuery library and E1) are properly imported in your page.

And Try this.

DEMO: http://jsfiddle.net/h9Veg/

$(document).ready(function(){
    $("#contenedor").mouseenter(function() { mostrarTexto(); });

    $("#contenedor").mouseleave(function() { ocultarTexto(); });
});


    var mostrarTexto=function(evento){
        $("#mostrador").css("display","block");
    }

    var ocultarTexto=function(evento){
        $("#mostrador").css("display","none");
    }
kpmDev
  • 1,330
  • 1
  • 10
  • 28
0

Not sure if this will help, but you close your <script> tag twice.

<script type="text/javascript" src="E1.js" />
</script>

Should be

<script type="text/javascript" src="E1.js">
</script>
Magnus Engdal
  • 5,446
  • 3
  • 31
  • 50
0

This is the possible solution:

$(document).ready(function(){
    $("#contenedor").mouseenter(function(evento) {
        mostrarTexto(evento);
    });

    $("#contenedor").mouseleave(function(evento) {
        ocultarTexto(evento);
    });
});


var mostrarTexto=function(evento){
    $("#mostrador").css("display","block");
}

var ocultarTexto=function(evento){
    $("#mostrador").css("display","none");
}
Pulkit Mittal
  • 5,916
  • 5
  • 21
  • 28
0

Your code is kinda wierd.

Why do you use

var mostrarTexto=function(evento){
    $("#mostrador").css("display","block");
}

why not

function mostrarTexto(evento){
    $("#mostrador").css("display","block");
}

and if you want to be falsifiable use the assignment bound to document root, not best practice but it helps when code gets ugly

document.mostrarTexto=function(evento){
    $("#mostrador").css("display","block");
}

And BEFORE all.. check if your function exists, and the scope of it. Because scope is a tricky thing.

Sangoku
  • 1,588
  • 2
  • 21
  • 50