我有一个追加
$("#accordionWiki").append("
<div class='panel panel-default' id='headingWiki_"+i+"'>
<div class='panel-body'> FUNCTION </div>
</div>"
);
和类似
的功能function wikiText() {
$.getJSON("https://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text&callback=?", {..
}
我试图把wikiText();在我写的 FUNCTION 的附加内部,但是我出错了,也许是串联,不确定。
答案 0 :(得分:0)
你不能附加你的函数的结果,因为你在该函数中使用ajax调用是异步的,只需在追加后调用它:
function(data) {
$("#accordionWiki").empty();
$.each(data.query.search, function(i, item) {
$("#accordionWiki").append("<div class='panel panel-default'><div class='panel-heading' role='tab' id='headingWiki_"+i+"'><h3 class='panel-title'><a role='button' data-toggle='collapse' data-parent='#accordionWiki' href='#collapseWiki_"+i+"' aria-expanded='true' aria-controls='collapseWiki_"+i+"'>"+ item.title.replace(" ", "_") + "<br><small>" + item.snippet + "</small></a></h3></div><div id='collapseWiki_"+i+"' class='panel-collapse collapse' role='tabpanel' aria-labelledby='headingWiki_"+i+"'><div class='panel-body'></div></div></div></div>");
wikiText('headingWiki_'+i);
});
更改wiki函数以仅更新循环中的当前元素:
function wikiText(el) {
$el = $('#'+el);
$.getJSON("https://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text&callback=?", {
page: $(e.relatedTarget).find('h4').text()
}, function(data) {
var markup = data.parse.text["*"];
var blurb = $('<div></div>').html(markup);
blurb.find('a').each(function() {
$(this).replaceWith($(this).html());
});
blurb.find(".mw-editsection, #toc, .noprint, .thumb, img, table").remove();
$(el).find(".panel-title").html(articleName);
$(el).find(".panel-body").html($(blurb));
$(el).find(".panel-body").promise().done(function(){
$('.panel-body *[style]').attr('style', '');
$(el).find("table").addClass("table table-striped table-hover table-responsive").wrapAll('<div class="table"/>');
$(el).find("table, td, tr, th").removeAttr("style");
$(el).find("img").addClass("img-responsive");
$(el).find(".panel-body").css({opacity: 0.0, visibility: "visible"}).animate({opacity: 1.0});
saveWiki();
$(el).find(".panel-body div *").attr("contenteditable", "true");
});
});
};
答案 1 :(得分:0)
$("body").append("<div class='panel panel-default' id='headingWiki_'><div class='panel-body'> "+wikiText()+" </div></div>"
);
function wikiText() {
return "asd";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
像这样的东西
答案 2 :(得分:0)
你试过这个:
function wikiText() {
var returnHTML=""+
"<div class='panel panel-default' id='headingWiki_"+i+"'> "+
" <div class='panel-body'> FUNCTION </div> "+
" </div>";
return returnHTML;
}
$("#accordionWiki").append(wikiText());
我猜你需要你的I作为wikiText(I)函数的参数