【问题标题】:Display the content of a HTML file on a HTML page onclick of link单击链接时在 HTML 页面上显示 HTML 文件的内容
【发布时间】:2014-08-17 04:25:33
【问题描述】:

我在 我的本地目录 C:** 中有一个 HTML 文件,我想在我的 HTML 页面上提供一个名为 **status 的链接,单击该链接会显示 HTML 文件的内容(具有HTML 页面上的数据网格表)。

谁能建议我如何实现这一目标?

P.S: 下面是具有数据网格的 HTML 文件的代码,我想在单击名为 Status 的链接时将其显示在纯 HTML 页面上。

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Server status</title>
    <link rel="stylesheet" type="text/css" href="http://www.jeasyui.com/easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="http://www.jeasyui.com/easyui/themes/icon.css">
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="http://www.jeasyui.com/easyui/jquery.easyui.min.js"></script>
</head>
<body>
    <table id="tt" class="easyui-datagrid" style="width:380px;height:auto;">
        <thead>
            <tr>
                <th field="name1" width="80">Status</th>                
            </tr>                          
        </thead>                           
        <tbody>                            
            <tr>                           
                <td>Australia</td>                      
            </tr>                          
            <tr>                           
                <td>Canada</td>                        
            </tr>                          
            <tr>                           
                   <td>USA</td>                                     
            </tr>                          
            <tr>                           
                <td>UK</td>                       
            </tr>                          
        </tbody>                           
    </table>        
</body>
</html>

问题代码

<html>
 <script type="text/javascript" src="jquery-1.2.6.js"></script>
   <script type="text/javascript"> 
 $(document).ready(function() {

   $('.click').on("click", function(e){
        e.preventDefault(); // cancel the default a tag event.

        $.get( "datagrid.html", function( data ) {
          $(".result").html( data );
        });

   });
 });
</script>
<body style="background-color:gray;">
<div id="wrapper">

  <div id="tabContainer">
    <div id="tabs">
      <ul>
        <li id="tabHeader_1">Status</li>
      </ul>
    </div>
    <div id="tabscontent">
      <div class="tabpage" id="tabpage_1">
      <marquee behavior="scroll" bgcolor="yellow" loop="-1" width="35%"><i><font color="Red"><strong>One server is down...</strong></font></i></marquee>   
     <a href="http://localhost:8080/monitor/datagrid.html" class="click"><font color="Black">click me</font></a>
<div class="result"></div>
</body>
</html>

【问题讨论】:

  • 您可以使用 jQuery 的 get() 方法 - api.jquery.com/jquery.get 然后将该 HTML 附加到页面。
  • 我对 Jquery 概念完全陌生,您能为我的上述代码提供一些示例或示例代码吗?

标签: jquery html datagrid


【解决方案1】:

类似这样的:

HTML:

<a href="#" class="click">click me </a>
<div class="result"></div>

jQuery

<script>
 $(document).ready(function() {

   $('.click').on("click", function(e){
        e.preventDefault(); // cancel the default a tag event.

        $.get( "my-file.html", function( data ) {
          $(".result").html( data );
        });

   });
 });
</script>

解释:

我们有一个链接,它绑定了一个点击事件,然后在点击时,我们将向我们的.html文件发出Ajax请求,然后我们将返回的data附加到results div ,

注意:要使其工作 - 您需要通过网络服务器(如 WAMP)运行它,而不是通过 file:// 协议。

【讨论】:

  • 我尝试在链接 localhost:8080/monitor/datagrid.html" class"click"> 中使用localhost:8080/monitor/datagrid.html,但它对我不起作用。通过在 apache tomcat 应用服务器中托管我的 html 文件数据网格,我做错了吗?
  • @ScriptLearner 你能用你正在使用的最新代码更新你的帖子吗?谢谢。
  • 我已在以下部分添加了我正在使用建议脚本的代码部分:我的原始帖子线程中的问题代码
  • 您使用的 jQuery 版本是 2008 年发布的,您不妨试试最新版本 ;-)
猜你喜欢
  • 2013-02-03
  • 2017-03-07
  • 2013-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-07
  • 2015-05-27
相关资源
最近更新 更多