簡體   English   中英

如何使用JQuery引用單獨的html文件的ID?

[英]How can I reference the ID of a separate html file using JQuery?

<div id="container">
    <nav>
        <ul class="clearfix" id="cfix">
            <li class="icon" id="icon">
                <a href="javascript:void(0);" onclick="myMenu()">&#9776; 
                    Menu</a>
            </li>
            <div id="drop">
                <li><a href="index.html#about">About Me</a></li>
                <li><a href="index.html#resume">Resume</a></li>
                <li><a href="index.html#music">Music</a></li>
                <li><a href="index.html#contact">Contact</a></li>
                <li><a href="blog.html">Blog</a></li>
            </div>
        </ul>
    </nav>
</div>

<script>
    $(function() {
        $('#drop li').click(function() {
          $(this).addClass('selected').siblings().removeClass('selected');
        });
    });
</script>

我試圖激活從bio.html單獨的html文件[index.html]中引用的ID的類[selected]。 上面的代碼在index.html上有效,但在bio.html上無效。

我嘗試將[href^='index.html#']到JQuery選擇器,但無濟於事。

如何引用單獨的html的ID?

我再說一遍,盡管您一開始就很清楚,但對您的問題仍然不明白,我感到非常抱歉。

要實現您的要求,只需將其添加到index.html文件中:

<script>
    $(document).ready(function(){
        $("#drop").children().children("[href='index.html"+document.location.hash+"']").addClass('selected');
});
</script>

我希望這是您要尋找的。

您不能從另一個HTML文件引用樣式。

您應該將樣式寫入.css文件,並將該文件包含在要在其中使用的每個頁面的<head>元素中。

<head>
    <link rel="stylesheet" type="text/css" href="path_to_css_file.css" />
</head>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM