Jquery 如何确保在使用.html()创建accordion之前已加载accordion
我遇到了一个问题,在启动accordion之前,accordion html首先使用.html()注入到标记上,从而使注入的html看起来像普通html 这是我的脚本代码:Jquery 如何确保在使用.html()创建accordion之前已加载accordion,jquery,jquery-ui,jquery-ui-accordion,Jquery,Jquery Ui,Jquery Ui Accordion,我遇到了一个问题,在启动accordion之前,accordion html首先使用.html()注入到标记上,从而使注入的html看起来像普通html 这是我的脚本代码: $(function() { $( "#accordion1" ).accordion({ heightStyle: "content", collapsible: true, active:false, activate:"refresh",
$(function() {
$( "#accordion1" ).accordion({
heightStyle: "content",
collapsible: true,
active:false,
activate:"refresh",
animate:false
});
$.getJSON( 'http://localhost/media_books/index.php/new_books.json?provider_id=1&limit=99&offset=1')
.done(function( json ) {
var html = '<div id="accordion1" style="font-size:smaller;" class = "accordion">';
for ( var obj in json.data) {
var att = json.data[obj].attributes;
html += '<h3>' + att.title + '</h3>';
html += '<div >';
html += "<p>" + att.author + "</p>";
html += '</div>';
}
html += '</div>';
$("#tabs_1").html(html);
});
});
$(函数(){
美元(“#手风琴1”)。手风琴({
高度样式:“内容”,
可折叠的:是的,
活动:错误,
激活:“刷新”,
动画:错误
});
$.getJSON('http://localhost/media_books/index.php/new_books.json?provider_id=1&limit=99&offset=1')
.done(函数(json){
var html='';
for(json.data中的var obj){
var att=json.data[obj].属性;
html+=''+att.title+'';
html+='';
html+=“”+att.author+“”;
html+='';
}
html+='';
$(“#tabs_1”).html(html);
});
});
也许您需要创建事件,该事件在手风琴创建后触发-->只需将其放入.done()中即可
$(函数(){
$.getJSON('http://localhost/media_books/index.php/new_books.json?provider_id=1&limit=99&offset=1')
.done(函数(json){
var html='';
for(json.data中的var obj){
var att=json.data[obj].属性;
html+=''+att.title+'';
html+='';
html+=“”+att.author+“”;
html+='';
}
html+='';
$(“#tabs_1”).html(html);
美元(“#手风琴1”)。手风琴({
高度样式:“内容”,
可折叠的:是的,
活动:错误,
激活:“刷新”,
动画:错误
});
});
});
为什么在这里?因为:
如何确保在使用.html()创建accordion之前已加载accordion
从字面上讲,您的意思是等到my.getJSON()
是.done()
时,首先插入.html()
,然后执行$.accordio()
。上面的代码准确地翻译了它。不起作用,先生,只是澄清一下,它们都已经在$function({})中了@BryanPosas我知道,但是你移动了$(“#accordion 1”)。accordion(…)
部分在.done()
中吗?我真的很抱歉,先生,我从来没有注意到你的意思。在.getjson中完成了非常感谢先生
$(function() {
$.getJSON( 'http://localhost/media_books/index.php/new_books.json?provider_id=1&limit=99&offset=1')
.done(function( json ) {
var html = '<div id="accordion1" style="font-size:smaller;" class = "accordion">';
for ( var obj in json.data) {
var att = json.data[obj].attributes;
html += '<h3>' + att.title + '</h3>';
html += '<div >';
html += "<p>" + att.author + "</p>";
html += '</div>';
}
html += '</div>';
$("#tabs_1").html(html);
$( "#accordion1" ).accordion({
heightStyle: "content",
collapsible: true,
active:false,
activate:"refresh",
animate:false
});
});
});