![](/img/trans.png)
[英]javascript-Replace one div(created dynamically) with other div(created dynamically) on click of button
[英]Javascript Dynamically replace div content on button click
我正在尝试制作HTML5应用,并且我需要根据菜单选择动态地更改具有不同html文件的div的内容。 我当前拥有的代码将加载菜单,但是,当我单击按钮时,会收到一条短消息“错误加载页面”,该消息几秒钟后消失。 我自己弄弄它,无法完全弄清楚出了什么问题,所以我将显示当前的代码。
index.html-
<body>
<div data-role="page" id="index">
<div id="wrapper">
<div data-role="controlgroup" data-corners="false" data-position="left">
<a href="/html/home.html" id="home" data-role="button">HOME</a>
<a href="/html/tracking.html" id="track" data-role="button">TRACKING</a>
<a href="/html/favourites.html" id="fav" data-role="button">FAVOURITES</a>
<a href="/html/setting.html" id="set" data-role="button">SETTINGS</a>
<a href="/html/login.html" id="login" data-role="button">LOG IN</a>
</div>
<div id="main">
<div id="header" data-role="header" data-theme="b">
<a id="bars-button" data-icon="bars" class="ui-btn-right" style="margin-top:0px;" href="#navpanel">MENU</a>
</div>
<div id="dMain"> </div>
</div>
</div>
</div>
<script>
//$(document).ready(function(){
//$('#home').click(function(){
// $('#dMain').load('/html/home.html');
// });
//});
</script>
</body>
home.html-
<body>
<div data-role="page" id="home">
<h1>Hello</h1>
<h2>This is the home page</h2>
<p>it will contain about us information</p>
</div>
</body>
我已进行了一些零零碎碎的修改,因此它目前不在工作状态,但是有关如何修复/改进此代码的任何建议将不胜感激。
假设您已正确加载了jquery库,如果未加载jquery 1.11和jquery ui 1.10.3以及主题 Try,则该库支持ajax,
<script>
$(document).ready(function(){
$('#home').click(function(e){
$('#dMain').load('/html/home.html');
e.preventDefault();
});
});
</script>
尝试这个
$(function() {
$('#home').click(function(e) {
e.preventDefault();
$.ajax({
type: 'GET',
url: '/html/home.html',
dataType: 'html',
success: function(response) {
$('#dMain').html(response);
}
});
});
});
我放置在home.html文件中的错误。 div中的一个不正确的语句正在影响其显示能力。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.