The problem about jquery $.ajax get value xml file
I want use $.ajax get a url string from a xml file ,then with the url getted,I insert it into a style link ,then let the link insert into the <head>
,I write code like this:
$.ajax({
type: "get",
url: "Database/App_all.xml",
dataType: "html",
timeout: 2000,
success: function (xml) {
var $tid='id-5';
//alert($tid);
var $temp_private_css = $(xml).f开发者_如何学Cind("app[id='" + $tid + "']").find("css").text();
if ($temp_private_css.length > 0) {
//alert($temp_private_css);
$('head').append('<link href="' + $temp_private_css + '" rel="Stylesheet" type="text/css" />');
}
},
error: function () { }
});
However ,the result is in my firebug
<link type="text/css" rel="Stylesheet" href="' + $temp_private_css + '">
I use alert function to see whether the $temp_private_css get value,it shows correct like "Database/css/test.css",it just could't insert into the head
why this happened?How can I solve this problem?
You need to call that on document ready, like:
$(document).ready(function()
{
$.ajax({
type: "get",
url: "Database/App_all.xml",
dataType: "html",
timeout: 2000,
success: function (xml) {
var $tid='id-5';
//alert($tid);
var $temp_private_css = $(xml).find("app[id='" + $tid + "']").find("css").text();
if ($temp_private_css.length > 0) {
//alert($temp_private_css);
$('head').append('<link href="' + $temp_private_css + '" rel="Stylesheet" type="text/css" />');
}
},
error: function () { }
});
});
So that it adds the proper stylesheet to the DOM. Hope this helps.
精彩评论