你好,是一个可能得到的背景图片url从不同的页面与ajax?我试过这个,但它似乎没有工作。谢谢你的帮助。
var test = jQuery('#el').load('https://www.example.com/somepage/' .some-element).css('background-image');
console.log(test);
或者
jQuery.ajax({
url: 'https://www.example.com/somepage/',
type: 'get',
dataType: 'text',
success: function(data, status) {
console.log("Status: "+status+"\nData: "+data);
result = data;
var img = jQuery('.some-element').css('background-image');
console.log(img);
}
});
两个都是一样的。如何用javascript从另一个页面获取背景图片url?
它不是ajax,但它的解决方案是你可能需要的。
function loadBackground(url, query) {
return new Promise(function (resolve, reject) {
var iframe = document.createElement("iframe");
iframe.onload = function () {
var el = iframe.contentDocument.querySelector(query);
if (el) {
var style = iframe.contentWindow.getComputedStyle(el);
resolve(style.getPropertyValue("background-image"));
} else {
reject(new Error("Element not found"));
}
document.body.removeChild(iframe);
};
iframe.style.width = 0;
iframe.style.height = 0;
iframe.style.border = 0;
iframe.src = url;
document.body.appendChild(iframe);
});
}
loadBackground("https://www.example.com/somepage/", ".some-element").then(function (bg) {
console.log(bg);
});