[英]get the title from a context
如何选择上下文标题? 我想选择它来更改页面标题。
var handler = function(context){
document.title = //context's title
....
}
//sample context from ajax request
var context = '<!DOCTYPE html><html>\
<head><title>Hello</title></head>\
<body>\
<a href="#">Click Here</a><p>This is my sentence.</p>\
</body></html>';
.ajax{(
...
success: function(data){
handler(data);
}
});
编辑 :我忘记了doctype,以防万一是必要的。 上下文来自AJAX请求。
jQuery $函数的第二个参数是上下文,因此您可以尝试$("title",$(context)).text()
这应该做 :
var title = $(context).eq(0).text()
var handler = function(context){
$xml=$.parseXML(context);
console.log($($xml).find('title').text());
}
var context = '<html><head><title>Hello</title></head><body><a href="#">Click Here</a><p>This is my sentence.</p></body></html>';
handler(context);
我不知道为什么,但是没有人提及。 这是一种在响应中搜索元素的流行方法 :
$(function() {
var context = '<html><head><title>Hello</title></head><body><a href="#">Click Here</a><p>This is my sentence.</p>< /body></html > ';
alert($(context).filter("title").html()); //Use filter to get elements you want
alert($(context).filter("a").html()); //Will get HTML of that link (<a>)
alert($(context).filter("body > p").html()); //HTML of <p>
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.