javascript 如何使用jQuery获取标题标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23681427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get title tag using jQuery?
提问by Sushil Kumar
I have some html in string form.
我有一些字符串形式的 html。
var html = "
<html>
<head>
<title>
Some Text
</title>
</head>
<body>
<h1>
My First Heading
</h1>
<p>
My first paragraph.
</p>
</body>
</html>
";
It has title tag inside it. How can I get the text inside title tag using jquery or javascript?
它里面有标题标签。如何使用 jquery 或 javascript 获取标题标签内的文本?
回答by hsz
Just try with:
只需尝试:
var title = $(html).filter('title').text();
回答by jAndy
Just because no one mentioned it so far, you could also use document.implementation.createHTMLDocument()
for that purpose.
仅仅因为到目前为止没有人提到它,您也可以document.implementation.createHTMLDocument()
用于该目的。
var domstr = "<html><head><title>Some Text</title></head><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>";
doc = document.implementation.createHTMLDocument('1337');
doc.documentElement.innerHTML = domstr;
alert( doc.title );
回答by Loktar
This is my crack at it. Create a documentFragmentadd an element to it and use querySelector
to get the element and then textContent
to get the text.
这是我的窍门。创建一个documentFragment向它添加一个元素并使用它querySelector
来获取元素然后textContent
获取文本。
var html = "<html><head><title>Some Text</title></head><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>",
docFrag = document.createDocumentFragment(),
el = document.createElement('html');
el.innerHTML = html;
docFrag.appendChild(el);
var text = docFrag.querySelector('title').textContent;
回答by adeneo
How about using the DOMparser
如何使用DOMparser
var html = "<html><head><title>Some Text</title></head><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>",
doc = (new DOMParser()).parseFromString(html, 'text/html');
title = doc.title;
Note:This is not supported on any version of Safari
注意:任何版本的 Safari 都不支持此功能
回答by saiid
In JavaScript, simply
在 JavaScript 中,只需
document.title
For sample code
对于示例代码
<html>
<head>
<title>My Page Title</title>
</head>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = document.title;
</script>
</body>
</html>
回答by griffon vulture
html.match(/<title>(.*)<\/title>/)[1]
回答by ach
Obviously not preferred if you can use jQuery, but for pure Javascript:
如果您可以使用 jQuery,但对于纯 Javascript,显然不是首选:
/<title>(.*)<\/title>/.exec(html)[1]
回答by Jalay
html.substring(html.indexOf('<title>')+7, html.indexOf('</title>'))
回答by Ramesh
You can get that text by using this jQuery selector.
您可以使用此 jQuery 选择器获取该文本。
$(function(){
var html = "<html><head><title>Some Text</title></head><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>";
var titleHTML = $(html).find("title").html();
alert(titleHTML);
});
It will alert "Some Text"
它会提醒“一些文本”
回答by Fahim Hossain
You can also try this:
你也可以试试这个:
$('head > title').text();