Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将.load()上的页面标题更改为jQuery中已加载页面的新标题_Javascript_Jquery_Html - Fatal编程技术网

Javascript 如何将.load()上的页面标题更改为jQuery中已加载页面的新标题

Javascript 如何将.load()上的页面标题更改为jQuery中已加载页面的新标题,javascript,jquery,html,Javascript,Jquery,Html,我对.load()和$.ajax感到困惑。我的index.html中有以下jQuery代码: $('.load_ext').click(function(e) { var url = $(this).attr("href"); parentContainer.append($(document.createElement("div")).load(url + ' #content_to_load').html('<p class="loading">Loadi

我对
.load()
$.ajax
感到困惑。我的
index.html
中有以下jQuery代码:

$('.load_ext').click(function(e) {    
    var url = $(this).attr("href");
    parentContainer.append($(document.createElement("div")).load(url + ' #content_to_load').html('<p class="loading">Loading&#8230;</p>').hide().fadeIn('slow'));
})
没有任何运气。它给了我当前页面的标题。如何通过执行以下操作来替换标题:

var url = $(this).attr("href");
parentContainer.append($(document.createElement("div")).load(url + ' #content_to_load', function() { 
     console.log($(document).attr('title'));
}).html('<p class="loading">Loading&#8230;</p>').hide().fadeIn('slow'));
$('title').text(newPageTitle);
谢谢


编辑: 多亏了@Jeff Schafer、@dystroy和@zeroflagL,我才解决了这个问题。以下是更改后的代码:

var url = $(this).attr("href");
parentContainer.append($(document.createElement("div")).load(url + ' #content_to_load', function(responseText) {
  var title = responseText.match(/<title>([^<]*)/)[1];
  document.title = title;
}).html('<p class="loading">Loading&#8230;</p>').hide().fadeIn('slow'));
var url=$(this.attr(“href”);
parentContainer.append($(document.createElement(“div”)).load(url+'#内容_to_load',函数(responseText){

var title=responseText.match(/([^尝试使用此代码更改页面标题

document.title = newPageTitle;

希望对您有所帮助

您可以通过responseText获得新页面的标题:

.load(url+)#content_to_load',function(responseText){


repsonseText
实际上是文本,但它是页面的源代码。例如,您可以使用正则表达式提取
test.html
的标题。然后您可以使用
document.title=newTitle
更改标题
load
的主要问题是jQuery在构建DOM片段时,剥离正文以外的内容。您可以使用此选项获取远程页面的标题:

$.get(url, function(html){
    var matches = html.match(/<title>([^<]*)/);
    if (matches.length>0) {
        var title = matches[1];
        document.title = title;
    }
});
$.get(url、函数(html){
var matches=html.match(/([^0){
var title=匹配项[1];
document.title=标题;
}
});

请注意,如果远程页面来自不同的来源,并且不特别允许跨域请求,则此操作将不起作用。

@Dom我尝试了此操作,但他们使用
#title
id来查找标题。我想要该页面的
尝试在该.load的回调中设置新的document.title。
已选择答案。您是c吗onfusing和两者都是不同的东西。
document.title;
将为您提供页面的当前标题。对于此页面,它将是:“javascript-如何使用jQuery-Stack Overflow更改.load()上的页面标题”您可以通过执行以下操作来更改它;document.title=“这是我的页面新标题”;.load()通常剥离文档
html
title
head
标记。ref我想用我正在处理的页面的标题替换标题loading@Alex你能得到新的页面标题吗?@Alex iBlue正在给你一个如何使用
文档的例子。标题
。使用这些信息并应用到你的代码中。天哪!!你是一个失败的人请任何人解释一下。我的回答有4张反对票。谢谢你没有发布与其他人相同的答案。我把这个放在了评论中,但似乎被忽略了。@JeffShaver这是一个很好的评论Jeff:)。我在看到你的评论后删除了我的答案。@dystroy似乎对这个问题的答案有很多奇怪的否决,没有任何评论…我给了你我的+1。我用了dystroy答案中的正则表达式。是的,我不知道是谁疯了。我给了你+1。
$.get(url, function(html){
    var matches = html.match(/<title>([^<]*)/);
    if (matches.length>0) {
        var title = matches[1];
        document.title = title;
    }
});