我需要根据 URL 获取网页的标题。我的方法是使用 jQuery:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
$.ajax({url: 'http://www.google.com', success: function (data) {
console.log($('title', $(data)).html());
}});
</script>
</body>
</html>
但是它不知何故失败了,它只返回一个未定义
值和一个错误:
"Failed to load resource file:///C:/images/srpr/logo4w.png"
我做错了吗?
最佳答案
http://jsbin.com/owavux/2/edit
(SAMEORIGIN
的工作原因)
$.ajax({
url: 'http://jsbin.com/owavux/1/edit',
success: function (data) {
var title = $(data).filter("title").text();
alert( title );
// To set that title to your current page do like:
document.title = title;
}
});
关于jquery - 如何使用 jQuery 从请求的 html 中获取 <title> 标签的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17395641/