如何加载来自另一个网页的背景图片? [英] How do I load the background image from another page?
问题描述
我创建一个页面加载使用jQuery的像这样的其他页面的内容:
$('#NewPage公司)。负载(example.html'+'#pageContent',函数(){
loadComplete();
});
这一切工作正常。
现在我想要做的是改变当前页面的背景图片,我从加载页面的背景图像。
这是我在做什么,但现在我不能为我的生命得到它的工作:
$。获得(example.html',功能(数据){
变种pageHTML = $(数据);
VAR pageBody = pageHTML $('身体')。
警报(pageBody.attr(背景));
});
我在做什么错了?
谢谢,
-Ben
感谢大家的答复。
我开始另一个线程<一href="http://stackoverflow.com/questions/2883725/how-do-i-extract-a-background-value-from-a-string-containing-html-in-javascript">here在希望我能做到这一点不同的方式,我得到了一个<一个href="http://stackoverflow.com/questions/2883725/how-do-i-extract-a-background-value-from-a-string-containing-html-in-javascript/2884581#2884581">working从泗门Echholt 解决方案,为求人们寻找那就是:
我拼凑一个正则表达式来做到这一点, 这将搜索数据串 变量(包含HTML)为 body标签的背景属性。 正则表达式就是从这里偷走, 修改了一下。我还是新来 正则表达式,所以我想这是可以做到更多 流利,但它仍然得到了那份工作 完成
VAR数据= / *你的HTML * /; VAR正则表达式= /body.*background = ['](((?。?!?['] \ S +(?:?\ S +)= | [&gt;中。']))+) ?'] /; VAR的结果= regex.exec(数据); 如果(result.length→1){ VAR背景=结果[1]; 警报(背景); } 其他 { //没有比赛 }
如果你使用的答案,请投上了他的<一个href="http://stackoverflow.com/questions/2883725/how-do-i-extract-a-background-value-from-a-string-containing-html-in-javascript/2884581#2884581">here!
再次感谢!
-Ben
I'm creating a page that loads content from other pages using jQuery like this:
$('#newPage').load('example.html' + ' #pageContent', function() {
loadComplete();
});
That all works fine.
Now what I want to do is change the background image of the current page to the background image of the page I'm loading from.
This is what I'm doing now but I can't for the life of me get it to work:
$.get('example.html', function(data) {
var pageHTML = $(data);
var pageBody = pageHTML.$('body');
alert(pageBody.attr("background"));
});
What am I doing wrong??
Thanks,
-Ben
Thanks to all of you for your replies.
I started another thread here in hopes that I could do it a different way and I got a working solution from Simen Echholt, for the sake of people searching here it is:
I patched together a regex to do this, which will search the data string variable (containing the HTML) for the background attribute of the body tag. The regex is stolen from here and modified a bit. I'm still new to regex, so I guess it can be done more fluently, but it still gets the job done
var data = /* your html */; var regex = /body.*background=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?/; var result = regex.exec(data); if (result.length > 1) { var background = result[1]; alert(background); } else { //no match }
If you used that answer please vote him up over here!
Thanks again!
-Ben
这篇关于如何加载来自另一个网页的背景图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!