JQuery click() 无法正常工作?

标签 jquery html click mouseover

我在 Jquery 中点击时遇到问题!与 mouseover 相同的另一个事件可以正常工作,但 .click 却不能!它显示警报!并且增加宽度,但再次回到之前的宽度!

<!DOCTYPE html5/>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <script type='text/javascript' src="https://code.jquery.com/jquery-2.2.4.js"></script>
 <link rel="stylesheet" href="styles.css">
 <link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.min.css">
</head>

<body>
  <nav id="header" class="navbar navbar-default navbar-static">
    <ul class="nav navbar-nav" id="ul" dir="rtl">
      <li>
         <a href="" class="me">من !</a>
      </li>
      <li class="Active">
        <a href="">صفحه اصلی</a>
      </li>
      <li>
        <a href="">مهارت ها</a>
      </li>
     <li>
        <a href="">تماس با من</a>
      </li>
    </ul>
  </nav>
 <div id="about">
 </div>

  <ol class="console">
   <li>
     <span class="aden">[aden]~</span> Initialisation <span>(100%)
     </span>
   </li>
   <li>
     <span class="aden">[aden]~</span> &#8592 <span> for Left and 
     </span> &#8594; <span> for Right </span>
   </li>
   <li>
     <span class="aden">[aden]~</span> <span></span>
   </li>
 </ol>

 </body>
 <script type='text/javascript'>
   $(document).ready(function() {
     $(document).on('click', '.me', function() {
       alert("hi");
       $(".console").css("width", "400px");
     });
   });
 </script>

</html>

CSS:

@font-face {
  font-family: 'CaviarDreams';
  src: url('CaviarDreams.woff');
}

@font-face {
  font-family: 'gardesh';
  src: url('gardesh.otf');
}

@font-face {
  font-family: 'nazanin';
  src: url('nazanin.otf');
}

body,html {
  font-family: CaviarDreams;
}
#header {
  width: 100%;
  height: 70px;
  background-color: white;
  font-family: nazanin;
  font-size: 15px;
  float: right;
  border: none;
}

#header li {
  float: right;
}

#header .me {
  color: gray;
  border-radius: 60px;
  font-weight: bold;
}

#header li a {
  padding-top: 24px;
  padding-bottom: 24px;
  color: gray;
  font-weight: bold;
}

#header .Active a{
  color: black;
  background-color: white;
}

#header h1 {
  float: left;
  color: white;
  font-weight: bold;
  margin: 10px -10px;
}

#ul {
  float: right;
}


#about {
  position: fixed;
  right: 0;
  width: 300px;
  display: none;
  float: right;
  height: 673px;
  border: black solid;
  background-color: white;
}



.console {
  background-color: #000;
  width: 350px;
  height: auto;
  min-height: 134px;
  padding: 10px 40px;
  border-radius: 15px;
  list-style: none;
  float: right;
  margin: 350px 450px 0 0;
}

.aden {
  color: #fff;
}

ol.console > li {
  margin-top: 20px;
  margin-bottom: 5px;
}

最佳答案

$(document).ready(function() {
     $(".me").click(function(event) {
       event.preventDefault();
       $(".console").css("width", "400px");
     });
   });
<!DOCTYPE html5/>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <script type='text/javascript' src="https://code.jquery.com/jquery-2.2.4.js"></script>
 <link rel="stylesheet" href="styles.css">
 <link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.min.css">
</head>

<body>
  <nav id="header" class="navbar navbar-default navbar-static">
    <ul class="nav navbar-nav" id="ul" dir="rtl">
      <li>
         <a href="" class="me">من !</a>
      </li>
      <li class="Active">
        <a href="">صفحه اصلی</a>
      </li>
      <li>
        <a href="">مهارت ها</a>
      </li>
     <li>
        <a href="">تماس با من</a>
      </li>
    </ul>
  </nav>
 <div id="about">
 </div>

  <ol class="console">
   <li>
     <span class="aden">[aden]~</span> Initialisation <span>(100%)
     </span>
   </li>
   <li>
     <span class="aden">[aden]~</span> &#8592 <span> for Left and 
     </span> &#8594; <span> for Right </span>
   </li>
   <li>
     <span class="aden">[aden]~</span> <span></span>
   </li>
 </ol>
 </body>
</html>

它运行完美,我刚刚尝试了代码片段,您也可以检查它。

关于JQuery click() 无法正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44601988/

相关文章:

jquery - 如何禁用 fancybox 窗口背景中的操作

javascript - 滑动包装器可阻止页面上的所有点击事件

javascript - 如何在 Bootstrap datetimepicker 中限制最小-最大时间

html - 具有高度的固定列表

html - 如何更改这些按钮的位置?

jquery - 拦截复选框单击以确认更改

javascript - jQuery.load() : loading a div when click

javascript - 解析从 Controller 收到的 Json

javascript - 使 js 文件无法通过 Web 控制台访问

javascript - IE Javascript 点击问题