加载后将jQuery Mobile元素添加到页面中?
问题描述:
在这个例子的jsfiddle ...加载后将jQuery Mobile元素添加到页面中?
...我想添加一个div里面jQuery Mobile的样式按钮的页面加载后。页面加载时出现在页面上的大按钮看起来很棒......但是当我尝试添加另一个类似外观的按钮时(通过点击“添加另一个按钮”),它不会被设置为它应该(即使我'追加'通过jQuery的按钮正确的HTML代码)。
下面是完整的代码...
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>
</head>
<body>
<!-- Mark-up -->
<input id="addButton" type="button" value="Add Another Button">
<div style="width: 300px; height: 400px; top: 50px; left: 10px; position: absolute; border: 1px solid black;" >
<section id="page1" data-role="page">
<div class="content" data-role="content">
<a href="#" data-role="button">This is a button!</a>
</div>
</section>
</div>
<!-- Script -->
<script>
$(document).ready(function() {
$('#addButton').click(function() {
$('.content').append('<a href="#" data-role="button">This is another button!</a><br />');
});
});
</script>
</body>
</html>
缺少什么我在这里?我如何让jQuery Mobile识别对HTML的动态更新?
谢谢!
答
这里有一个工作示例:http://jsfiddle.net/Gajotres/2uerX/
$(document).on('pagebeforeshow', '#index', function(){
$('#addButton').click(function() {
$('.content').append('<a href="#" data-role="button">This is another button!</a><br />');
$('[data-role="button"]').button();
});
});
当你动态地添加新的内容JQM,JQM也必须加强新的内容。而在按键的情况下,它是用做:
$('[data-role="button"]').button();
要了解更多关于它看看我其他的答案/文章:jQuery Mobile: Markup Enhancement of dynamically added content
Gajotres ...我刚刚看了你的其他物品。很有帮助!如果你不介意的话,还有1个更快的问题。假设我想增强整个页面,*除1个特定项目外*。例如...在我的jsFiddle示例中,我想要增强大矩形div(大按钮)中的所有内容,但我不想增强顶部的小按钮(#addButton)。我会怎么做? – NotQuiteThereYet 2013-04-03 20:05:32
你会为该按钮添加一个data-role =“none”,这将阻止该按钮样式 – Gajotres 2013-04-03 20:14:07