关于jQuery中的一个很小的问题

查看:95
本文介绍了关于jQuery中的一个很小的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

今天室友问了一个遇到一个jquery的问题,感觉很简单,但是又没有找到答案,话不多说,直接上代码。

<script type="text/javascript">
function onClick(){
  alert("nihao");
}

function changeStyle() {
  if(typeof(showedMemu)=="undefined"){
    showedMemu = true;
  }
  if(showedMemu) {
    showedMemu = false;
      var obj = document.getElementById("css");
      obj.setAttribute("href","css/admin2.css");
  }
  else  {
    showedMemu = true;
      var obj = document.getElementById("css");
      obj.setAttribute("href","css/admin.css");
  }
}

$(document).ready(function () {
 $('.head-l').click(function(event) {
   /* Act on the event */
 });(function() {
  $('ol', this).slideDown(200);
  $(this).children('a:first').addClass("hov");
 }, function() {
  $('ol', this).slideUp(100);
  $(this).children('a:first').removeClass("hov");
 })

</script>

(代码应该是正确的)

在执行含有这个 script 的 html 代码的时候,打开后台,并不能找到changeStyle 和 onClick 这两个函数,而当把用jQuery写的这段代码注释掉之后,就可以找到这两个函数。

提供如下信息:

  • 把这两个用原生javascript写的函数和jQuery写的函数放在两个不同的<script>中,则可以在后台找到原生的函数

初步猜测是这个用jquery写的代码的问题,但是通过较长时间的网上搜寻,只知道用jquery写的这段代码是在文档完成之后才能执行的意思,但感觉这个解释并不能解决我的问题。

所以想要寻求大佬们的帮助,提供一个满意的答复(我感觉很多人看到这个问题也会有困惑)

解决方案

jQuery那段代码不正确,把你的代码格式化了,多个错误:

  1. ready后的(没有对应的关闭标签)

  2. 第5行开始()里的两个匿名函数不能用逗号分隔

$(document).ready(function() {
  $('.head-l').click(function(event) {
    /* Act on the event */
  });
  (function() {
      $('ol', this).slideDown(200);
      $(this).children('a:first').addClass("hov");
    },
    function() {
      $('ol', this).slideUp(100);
      $(this).children('a:first').removeClass("hov");
    }
  )

这篇关于关于jQuery中的一个很小的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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