浏览器无法识别javascript / jquery代码 [英] Browser doesn't recognize javascript / jquery code

查看:87
本文介绍了浏览器无法识别javascript / jquery代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在使用Kubuntu Linux的计算机上运行Firefox 27.0.1和Chrome 30.0.1599.114中的以下代码,并且没有任何反应。

html页面是从基于Spring MVC框架的Web应用程序中获取,并放置在WEB-INF / jsp文件夹中。有人可以在下面的代码中找到任何错误?

 <!DOCTYPE html> 
< html lang =en>
< head>
< meta charset =utf-8>
< meta http-equiv =X-UA-Compatiblecontent =IE = edge,chrome = 1>
< title> HorarioLivre< / title>

< script src =http://code.jquery.com/jquery-2.1.0.min.js>< / script>
< script>
$('a')。click(function(){
alert(clicou em um link);
});
< / script>

< link rel =stylesheethref =css / style-main.css>
< link rel =stylesheethref =css / style-popup.css>
< / head>
< body>
< header>
< div class =container>
< h1>< a href =#> HorarioLivre< / a>< / h1>
< nav>
< ul>
< li>< a href =listagem_evento.htmlclass =icon evento> Eventos< / a>< / li>
< li>< a href =cadastra_horario.htmlclass =icon horario> Cadastrar Horarios< / a>< / li>
< li>< a href =listagem_horario.htmlclass =icon horario> Listar Horarios< / a>< / li>
< li>< a href =listagem_usuario.htmlclass =icon usuario> Usuarios< / a>< / li>
< li>< a href =#> $ {usuario.nome}< / a>
< ul>
< li>< a href =usuario_perfil.htmlclass =icon perfil> Perfil< / a>< / li>
< li>< a href =usuario_config.htmlclass =icon settings> Configura& ccedil;& otilde; es< / a>< / li>
< li>< a href =#>注销< / a>< / li>
< / ul>
< / li>
< / ul>
< / nav>
< / div>
< / header>
< div id =results>
< a href =#id =close> Fechar< / a>
< div id =content>< / div>
< / div>
< / body>
< / html>


解决方案

您正在选择所有脚本运行时存在的元素(因为脚本运行在头部和所有 > 元素存在于主体中)并为它们做点什么。



将脚本移动到之前< / body> 或创建一个函数并在DOM准备好的时候运行它。


$ b

  jQuery(function() (
'('a')。click(function(){
alert(clicou em um link);
});
});

...那么会有实际的 a 元素你可以操纵。


I try run the following code in Firefox 27.0.1 and Chrome 30.0.1599.114 on a machine with Kubuntu Linux, and nothing happens.

The html page is part from a web application based on Spring MVC framework, and was placed in the folder WEB-INF/jsp. Someone can find any error in the code below?

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>HorarioLivre</title>

  <script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
  <script>
  $('a').click(function() {
      alert("clicou em um link");
  });
  </script>

  <link rel="stylesheet" href="css/style-main.css">
  <link rel="stylesheet" href="css/style-popup.css">
</head>
<body>
  <header>
    <div class="container">
      <h1><a href="#">HorarioLivre</a></h1>
      <nav>
        <ul>
          <li><a href="listagem_evento.html" class="icon evento">Eventos</a></li>
          <li><a href="cadastra_horario.html" class="icon horario">Cadastrar Horarios</a></li>
          <li><a href="listagem_horario.html" class="icon horario">Listar Horarios</a></li>
          <li><a href="listagem_usuario.html" class="icon usuario">Usuarios</a></li>
          <li><a href="#">${usuario.nome}</a>
            <ul>
                <li><a href="usuario_perfil.html" class="icon perfil">Perfil</a></li>
                <li><a href="usuario_config.html" class="icon settings">Configura&ccedil;&otilde;es</a></li>
                <li><a href="#">Logout</a></li>
            </ul>
          </li>
        </ul>
      </nav>
    </div>
  </header>
  <div id="results">
        <a href="#" id="close">Fechar</a>
        <div id="content"></div> 
  </div>
</body>
</html>

解决方案

You are selecting all the a elements that exist at the time the script runs (all zero of them since the script runs in the head and all the a elements exist in the body) and doing stuff to them.

Move the script to just before </body> or create a function and run it on DOM ready.

jQuery(function () {
    $('a').click(function() {
        alert("clicou em um link");
    });
});

… then there will be actual a elements you can manipulate.

这篇关于浏览器无法识别javascript / jquery代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆