Javascript 需要使用JQuery更改页面标题的帮助吗
我正在尝试使用以下代码更改标题:Javascript 需要使用JQuery更改页面标题的帮助吗,javascript,php,jquery,html,joomla,Javascript,Php,Jquery,Html,Joomla,我正在尝试使用以下代码更改标题: /* It gets should get the information stored in name */ $(document).ready(function(){ var Title = $("h2").each(function(){ if($(this).attr("itemprop")=="name"){ document.title=($(this).html()); }
/* It gets should get the information stored in name */
$(document).ready(function(){
var Title = $("h2").each(function(){
if($(this).attr("itemprop")=="name"){
document.title=($(this).html());
}
});
});
它不起作用,但是为什么呢?我能够从name值中获取信息,但无法将其存储在title中
我想替换标题中存储的所有信息。您可以尝试以下操作:
$(document).attr('title', 'Hello World')
您的
文档。标题应该有效。您也可以使用$(“title”).html(“newtitle”)代码>
我认为问题是,代码可能没有执行。如果if语句成功,是否尝试了控制台输出
我认为更好的解决办法是
$(document).ready(function(){
var title = $("h2[itemprop='name']").html();
$("title").html(title);
});
如果有多个h2具有此itemprop属性,则此代码必须稍微更改。请同时显示HTML$(document).attr(“title”,“New title”)document.title=“some value”
工作正常。问题是什么还不清楚。您的文档中可能没有与IF语句匹配的内容。欢迎使用SO。请访问,了解如何提问以及提问内容。提示:显示HTML。此外,您还可以删除var Title=
anywayNeed HTML,以便根据HTML&要求获得正确答案。控制台不会返回任何信息,因此我假设它没有遇到任何问题。我已经使用$(document.body).append($(this.html())对代码进行了测试,它将存储在页面名称中的信息打印出来。现在我只想将它存储在title$(document.ready(function(){var title=$(“h2”).each(function(){if($(this.attr)(“itemprop”)==“name”){$(document.attr('title',我如何在这里获得name变量?')};});