dpr81047 2010-10-31 19:07
浏览 74
已采纳

使用javascript更改元素ID

I am loading in the following navbar html from a required PHP file:

    <ul id="navlist">
        <li id="active"><a href="#" id="current">Home</a></li>
        <li><a href="about.php">About</a></li>
        <li><a href="news.php">News</a></li>
        <li><a href="applying.php">Applying</a></li>
        <li><a href="current.php">Current <br />Residents</a></li>
        <li><a href="alumni.php">Alumni</a></li>
        <li><a href="contact.php">Contact</a></li>
        </ul>

Depending on the page that I am on (let's say I am on the alumni.php page) I want that list item to be given the ID "active"?

Edit: Here is my header.php code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link rel="stylesheet" href="styles/main.css" type="text/css" media="screen" charset="utf-8"/>
    <link rel="stylesheet" href="styles/navbar.css" type="text/css" media="screen" charset="utf-8"/>
    <title>some title</title>
</head>
<body>
    <div id="header">
        <div id="left">
            <img src="images/tree.png" alt="tree" width="87" height="98"></img>
        </div>
        <div id="right">
            <
        </div>
    </div>  
        <div id="navigation">
            <ul id="navlist">
            <li><a href="index.php" id="current">Home</a></li>
            <li><a href="about.php">About</a></li>
            <li><a href="news.php">News</a></li>
            <li><a href="applying.php">Applying</a></li>
            <li><a href="current.php">Current <br />Residents</a></li>
            <li><a href="alumni.php">Alumni</a></li>
            <li><a href="contact.php">Contact</a></li>
            </ul>
        </div>

I assume that I need to do this through Javascript once the page loads? How would I do this?

  • 写回答

5条回答 默认 最新

  • doufeixuan8882 2010-10-31 19:18
    关注

    as said in comment, PHP will be a better way.

    You can simple doing it like this :

    <?php
    
    $header = file_get_content('header.html');
    
    $page = 'about.php';
    
    $header = str_replace('<li><a href="'.$page.'">', '<li id="active"><a href="#">', $header);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮