我们正在将内容加载到 JQuery UI 选项卡中,该内容包含结构如下的 HTML:
<html lang="en-US">
<head>
<title>Product ABC</title>
</head>
<body>
Product description, images, order button, etc...
</body>
</html>
我需要获取
<title>
文本并将其设为浏览器选项卡标题,例如
document.title = $(loadedContent).filter('title').text();
但是 JQuery UI 选项卡控件似乎没有直接公开 ajax 响应。 “load”事件提供了对加载内容的面板的引用,但它似乎只包含
<body>
内容,<head>
已被删除。
有什么想法如何捕获 JQuery UI Tab 加载内容的
<title>
吗?
考虑以下示例:https://jsfiddle.net/Twisty/uxtnhmcp/
JavaScript
$(function() {
function getTabTitle(tab) {
console.log("Found Tab Title: " + tab.text().trim());
return tab.text().trim();
}
function setDocTitle(title) {
document.title = title;
console.log("Document Title change to '" + title + "'");
}
$("#tabs").tabs({
beforeLoad: function(e, ui) {
console.log(e.type);
ui.jqXHR.fail(function() {
ui.panel.html(
"Couldn't load this tab. We'll try to fix this as soon as possible. " +
"If this wouldn't be a demo.");
});
},
load: function(e, ui) {
if (ui.tab != undefined) {
setDocTitle(getTabTitle(ui.tab));
}
},
activate: function(e, ui) {
setDocTitle(getTabTitle(ui.newTab));
}
});
});
tab
或 newTab
对象将是 HTML 中的列表项。我们可以从元素中获取 .text()
并将其用作标题。