0

好的,这是我的问题。我有一个正在运行的 php 文件,它可以单独拉出我的菜单以及所有内容。因此,例如加载我的 Index.php 会加载 index.php menu.php 和一个 dropdown.js 文件。菜单正在工作并进行下拉。我的问题是我想调用一类导航并列出“li > a > id”名称,这些名称将是 Home、关于我们等……我尝试了很多不同的方法,但似乎无法弄清楚为什么我不能在 jquery 中找出一个简单的脚本来执行此操作。有人有什么建议吗?下面是我的菜单页面代码。

<div id="nav" class="nav">
  <ul id="sddm">
  <li><a class="navigation" id="Home" href="index.php" onMouseOver="mopen('m1')" onMouseOut="mclosetime()">Home</a>
  <div id="m1" onMouseOver="mcancelclosetime()" onMouseOut="mclosetime()"> 
  </div>
  </li>
  <li><a class="navigation" href="station_history.php" onMouseOver="mopen('m2')" onMouseOut="mclosetime()">Station History</a>
  <div id="m2" onMouseOver="mcancelclosetime()" onMouseOut="mclosetime()"> 
  </div>
  </li>
  <li>
  <a class="navigation" href="Apparatus.php" onMouseOver="mopen('m3')" onMouseOut="mclosetime()">Apparatus</a>
  <div id="m3" onMouseOver="mcancelclosetime()" onMouseOut="mclosetime()"> 
  <a class="navigation" href="truck_history.php">Truck History</a> 
  </div>
  </li>
  <li>
  <a class="navigation" href="photos.php" onMouseOver="mopen('m4')" onMouseOut="mclosetime()">Photos</a>
  <div id="m4" onMouseOver="mcancelclosetime()" onMouseOut="mclosetime()"> 
  </div>
  </li>
  <li>
  <a class="navigation" href="news_events.php" onMouseOver="mopen('m5')" onMouseOut="mclosetime()">News & Events</a>
  <div id="m5" onMouseOver="mcancelclosetime()" onMouseOut="mclosetime()"> 
  </div>
  </li>
  <li>
  <a class="navigation" href="Station_members.php" onMouseOver="mopen('m6')" onMouseOut="mclosetime()">Station Members</a>
  <div id="m6" onMouseOver="mcancelclosetime()" onMouseOut="mclosetime()"> 
  </div>
  </li>
  <li>
  <a class="navigation" href="education.php" onMouseOver="mopen('m7')" onMouseOut="mclosetime()">Education</a>
  <div id="m7" onMouseOver="mcancelclosetime()" onMouseOut="mclosetime()"> 
  <a class="navigation" href="SiteID">Station Tours</a>
  <a class="navigation" href="SiteID">Fire Extinguisher</a>
  <a class="navigation" href="SiteID">First Aid & CPR</a>
  <a class="navigation" href="SiteID">Smoke Alarms</a> 
  </div>
  </li>
  <li>
  <a class="navigation" href="SiteID" onMouseOver="mopen('m8')" onMouseOut="mclosetime()">Contact Us</a>
  <div id="m8" onMouseOver="mcancelclosetime()" onMouseOut="mclosetime()"> </div>
  </li>
  </ul>
  </div>
  </div>

这是我试图为不起作用的警报而做的代码。

//导航

$("a.navigation").click(function () {
    alert("hello"); 
}); 

终于弄清楚什么代码起作用了。这是我使用的,以防其他人遇到此问题。感谢所有帮助过的人!!!

//navigation 
$(document).ready(function() {
    $("a.navigation").click(function () {
        alert("hello"); 
    })

}); 
4

3 回答 3

0

使用 jQuery,您可以调用

$("a.navigation").click(function (e) {

    alert($(this).prop("id"));

})

小提琴出错了,但是当您单击“主页”时,它确实会向您显示警报。我想这就是你想要的。 http://jsfiddle.net/djwave28/M3vdf/1/

在第二种情况下,你可以做

var lid = Array();

 $("a.navigation").each(function (e) {

        lid.push($(this).prop("id"));

    })
console.log(lid);

这会将导航类元素的所有 id 放入 lid 数组中。

于 2013-03-21T04:30:19.610 回答
0

您将不得不首先阻止默认操作。尝试这个:

$("a.navigation").click(function (event) {
    event.preventDefault();
    alert("hello"); 
}); 
于 2013-03-21T04:25:53.957 回答
0

请查看此代码,它可以工作:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<div id="nav" class="nav">
  <ul id="sddm">
  <li><a class="navigation" id="Home" href="index.php" onMouseOver="mopen('m1')" onMouseOut="mclosetime()">Home</a>
  <div id="m1" onMouseOver="mcancelclosetime()" onMouseOut="mclosetime()"> 
  </div>
  </li>
  <li><a class="navigation" href="station_history.php" onMouseOver="mopen('m2')" onMouseOut="mclosetime()">Station History</a>
  <div id="m2" onMouseOver="mcancelclosetime()" onMouseOut="mclosetime()"> 
  </div>
  </li>
  <li>
  <a class="navigation" href="Apparatus.php" onMouseOver="mopen('m3')" onMouseOut="mclosetime()">Apparatus</a>
  <div id="m3" onMouseOver="mcancelclosetime()" onMouseOut="mclosetime()"> 
  <a class="navigation" href="truck_history.php">Truck History</a> 
  </div>
  </li>
  <li>
  <a class="navigation" href="photos.php" onMouseOver="mopen('m4')" onMouseOut="mclosetime()">Photos</a>
  <div id="m4" onMouseOver="mcancelclosetime()" onMouseOut="mclosetime()"> 
  </div>
  </li>
  <li>
  <a class="navigation" href="news_events.php" onMouseOver="mopen('m5')" onMouseOut="mclosetime()">News & Events</a>
  <div id="m5" onMouseOver="mcancelclosetime()" onMouseOut="mclosetime()"> 
  </div>
  </li>
  <li>
  <a class="navigation" href="Station_members.php" onMouseOver="mopen('m6')" onMouseOut="mclosetime()">Station Members</a>
  <div id="m6" onMouseOver="mcancelclosetime()" onMouseOut="mclosetime()"> 
  </div>
  </li>
  <li>
  <a class="navigation" href="education.php" onMouseOver="mopen('m7')" onMouseOut="mclosetime()">Education</a>
  <div id="m7" onMouseOver="mcancelclosetime()" onMouseOut="mclosetime()"> 
  <a class="navigation" href="SiteID">Station Tours</a>
  <a class="navigation" href="SiteID">Fire Extinguisher</a>
  <a class="navigation" href="SiteID">First Aid & CPR</a>
  <a class="navigation" href="SiteID">Smoke Alarms</a> 
  </div>
  </li>
  <li>
  <a class="navigation" href="SiteID" onMouseOver="mopen('m8')" onMouseOut="mclosetime()">Contact Us</a>
  <div id="m8" onMouseOver="mcancelclosetime()" onMouseOut="mclosetime()"> </div>
  </li>
  </ul>
  </div>
  </div>


  <script type="text/javascript">

    $(function(){
        $("a.navigation").click(function () {
                    event.preventDefault();
            alert("hello"); 
        }); 
    });

  </script>
于 2013-03-21T04:28:59.123 回答