简体   繁体   中英

With jQuery, can we actually refer to elements from a separate HTML file?

I am trying to make a tabbar view in my main.html so a section from the page switches as I click on different tab.

Just wanted to keep the code clean, I hope to write the section views from separate HTML files and "import" them.

Now, I am aware there is a way to import HTML as

<link rel="import" href="tab1.html">

And inside tab1.html, it looks like this

<!DOCTYPE html>
<html>
    <head>
        <meta charset="ISO-8859-1">
        <title>Insert title here</title>
    </head>
    <body>
        <div>
            <h1 id="sectionDiv">test test</h1>
        </div>
    </body>
</html>

but then in my main.html, I am not able to find text() value for $("#sectionDiv"). JQuery works fine for other things so there shouldn't be problems with the jQuery setup. And I do guess something I did wrong with the HTML import.

Could anyone help point that out?

Additional for clarification

Code of my main.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
    <title>Main Page</title>
    <link rel="import" href="carpools.html">
    <link rel="stylesheet" href="main.css"/>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.20/angular.min.js"></script>
    <script src="main.js"></script>
</head>
<body>
<div id="container">
    <div id="tabBar">
        <ul id="tabBarList">
            <li><a href="#" onclick="switchTab(0)">Tab1</a></li>
            <li><a href="#" onclick="switchTab(1)">Tab2</a></li>
            <li><a href="#" onclick="switchTab(2)">Tab3</a></li>
            <li><a href="#" onclick="switchTab(3)">Tab4</a></li>
            <li><a href="#" onclick="switchTab(4)">Tab5</a></li>
        </ul>           
    </div>


</div>
</body>
</html>

The onclick function switchTab() is meant to bring different section views ( id="sectionDiv" ) to the main. But apparently it is not able to identify it..

Try! Is this you looking for

Here is the main.html. I will call it as index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap 101 Template</title>
    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
      <style type="text/css">
          #tabBarList li {
              cursor: pointer;
          }
      </style>
  </head>
  <body>
      <div id="container">
          <div id="tabBar">
              <ul id="tabBarList">
                  <li><a class="menu" link="index.html">Tab1</a></li>
                  <li><a class="menu" link="/">Tab2</a></li>
                  <li><a class="menu" link="about.html">Tab3</a></li>
                  <li><a class="menu" link="/">Tab4</a></li>
              </ul>
          </div><!--end #container-->

          <section id="contents">
            <p>Other pages contents will loads to here.</p>
        </section><!--end #contents-->
      </div>

      <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
      <!-- Include all compiled plugins (below), or include individual files as needed -->
      <script src="js/bootstrap.min.js"></script>
      <script type="text/javascript">
          $(document).ready(function(){
              //alert("K");
              $(".menu").click(function(){
                var url = $(this).attr("link");
                //alert(url);
                //$( "#contents" ).fadeIn("slow").load(url+" #contents");
                $('#contents').fadeOut('slow', function(){
                    $('#contents').load(url+' #contents', function(){
                        $('#contents').fadeIn('slow');
                    });
                });
            });
          });
      </script>
  </body>
</html>

Here is the Tab3 page. I will call it as about.html

<section id="contents">
    <p>About Page Loaded</p>
</section>

Try This.

And let me know if you have any query or concerns. If you need my help. Please find me any of social network by searching yeshansachithak .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM