2017-04-01 23 views
0

我有一个滑块菜单,我想通过点击另一个页面加载。在Firefox的作​​品,但我想在任何浏览器中工作我的HTML?你可以帮我吗?谢谢。onClick无法在Chrome中使用?

的index.html:

<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
</head> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
<body> 
<div > 
    <a href="#" id="link1" >Vertical link 1</a> 
</div> 
<div id="content"></div> 
<script> 
    $("#link1").on("click", function() { 
    $("#content").load("test.html"); 
    }); 
</script> 
</body> 
</html> 

的test.html:

<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
</head> 
<body> 
<div> 
    <table> 
     <thead> 
      <tr> 
       <th scope="col">A</th> 
       <th scope="col">B</th> 
       <th scope="col">C</th> 
       <th scope="col">D</th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr> 
       <td>1</td> 
       <td>2</td> 
       <td>3</td> 
       <td>4</td> 
      </tr>   
     </tbody> 
    </table> 
</div> 
</body> 
</html> 
+0

无法生成它。你可以分享它不工作的任何小提琴吗? –

+0

我修改了这个问题,我想通过点击加载另一个页面 – Alex

+0

难道你不能简单地把test.html放在锚点的href里吗?它不可用吗? –

回答

0

如果内容是动态的,那么它需要被授权:

$(document).on("click", "#link1" , function() { 
    console.log('click') 
    $("#content").load("./resources/link1.html"); 
}); 
1

我觉得这有工作!

<div class="wrapper"> 
    <a href="#" id="link1" >Vertical link 1</a> 
    <a href="#" id="link2" >Vertical link 2</a> 
    <a href="#" id="link3" >Vertical link 3</a> 
    <a href="#" id="link4" >Vertical link 4</a> 
</div> 
<div id="content"></div> 

<script> 
    $('document').ready(function() { 
     $('.wrapper').children().on('click', function (e) { 
      e.preventDefault(); 
      $('#content').load($(this).attr('href')); 
     }); 
    }); 
</script> 
+0

纠正问题它是在Firefox上工作,但不是铬。这是chrome上的错误:“XMLHttpRequest无法加载file:// C:/test/test.html。只有协议方案支持跨源请求:http,数据,chrome,chrome扩展名,https。 – Alex

+0

@alex这意味着你应该在服务器上运行它,而不是在你的文件系统上运行 – Qchmqs

0

如果您导航到http://api.jquery.com/load/你会发现这条线:

描述:从服务器加载数据和返回的HTML放入匹配元素。

所以,如果你想使用负载基本上你应该从服务器火起来。

试试这个,如果你已经安装了python3上的命令提示符:

python3 -m http.server 9000

然后打开浏览器,然后导航至http://localhost:9000。点击它将工作的链接。

您可以从任何服务器做到这一点,但对于负载工作,你应该从服务器启动你的页面。

这是仅适用于Chrome。