简体   繁体   English

在jquery中定义和调用函数

[英]Defining and calling functions in jquery

I'm trying to call a function defined in an external .js using jQuery, but nothing happens. 我正在尝试使用jQuery调用外部 .js中定义的函数,但没有任何反应。 Here is my html: 这是我的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: 我的jQuery代码:

$(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 我已经尝试了很多我搜索过的东西,但我无法让它工作

Make sure the script files(jQuery library and E1) are properly imported in your page. 确保在页面中正确导入脚本文件(jQuery库和E1)。

And Try this. 试试这个。

DEMO : http://jsfiddle.net/h9Veg/ 演示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");
    }

Not sure if this will help, but you close your <script> tag twice. 不确定这是否有帮助,但您关闭<script>标记两次。

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

Should be 应该

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

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");
}

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 如果你想成为可证伪使用绑定到文档root的赋值,而不是最佳实践,但它有助于代码变得丑陋

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. 因为范围是一件棘手的事情。

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

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