1
我是jQuery/jQuery UI的新手。我试图在页面(sample.html)中创建多个标签,它通过ajax加载动态内容(temp.html)。期望的效果是temp.html中的内容应根据所选标签进行更改。我正在使用position将一些小部件放置在相对于彼此的位置。jQuery UI通过ajax位置标签()的内容仅适用于第一个标签
我面临的问题是,当我点击第一个标签,一切工作正常。当我点击第二个标签定位不起作用。我在下面添加了我的代码。
Sample.html
$(function() {
$("#tabs").tabs({
ajaxOptions : {
error : function(xhr, status, index, anchor) {
$(anchor.hash).html("Couldn't load this tab.");
},
cache : false
}
});
});
<div id="content" class="content">
<div id="tabs">
<ul>
<li><a href="temp.html">Tab 1</a></li>
<li><a href="temp.html">Tab 2</a></li>
</ul>
</div>
</div>
temp.html
#widget1 {
width: 150px;
height: 70px;
top: 20px;
left: 50px
}
#widget2 {
width: 150px;
height: 70px;
}
function resetPositions() {
$("#widget2").position({
my : "left top",
at : "right bottom",
of : "#widget1",
offset : "0 50"
});
}
$(function() {
$(".customAccordion").draggable();
$(".customAccordion").accordion({
collapsible : true,
fillSpace : false,
icons : {
header : "ui-icon-circle-arrow-e",
headerSelected : "ui-icon-circle-arrow-s"
}
});
resetPositions();
});
<div id="widget1" class="customAccordion">
<h3><a href="#">Widget 1</a></h3>
<div>Widget 1</div>
</div>
<div id="widget2" class="customAccordion">
<h3><a href="#">Widget 2</a></h3>
<div>Widget 2</div>
</div>
请不要让我知道,如果我犯错误。
我在两个不同的htmls有选项卡和手风琴。只有当我选择一个选项卡时,带有手风琴的页面才会通过ajax加载。所以我不认为有可能首先启动手风琴? – Sezhiyan 2012-08-14 12:54:03
我删除了手风琴,可拖动的divs。即使在那之后,定位也是错误的。我调试了firebug中的jquery代码并观察了这一点。对于第一个选项卡,Widget1的offset()返回大于0的数字。但是,当我单击第二个选项卡时,Widget1的偏移量返回0. – Sezhiyan 2012-08-14 13:02:17
mhh ok,你可以尝试添加resetPositions();在选择活动中,就像我在答案的最后部分所做的那样?您的问题可能与$(document).ready()事件有关,或者您的$(function()在动态加载的页面中不可靠。 – 2012-08-14 13:05:55