如何使用jQuery从请求的html中获取<title>标签的内容?</title>

时间:2013-06-30 23:06:17

标签: jquery

我需要根据其网址获取网页的标题。我这样做的方法是使用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>

然而,它以某种方式失败,它只返回一个undefined值和一个错误说:

"Failed to load resource file:///C:/images/srpr/logo4w.png"

我做错了吗?

1 个答案:

答案 0 :(得分:1)

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;
  }
});
相关问题