gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:52:55 25 4
gpt4 key购买 nike

我需要根据 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 中获取 &lt;title&gt; 标签的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17395641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com