我的网站标题中有以下内容
<link rel="stylesheet" id="swp-google-font-headline-css" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro&ver=4.5.3" type="text/css" media="all">
我想检索完整的<link>
内容如上图所示,使用jquery。
我试过了
var a = jQuery('#swp-google-font-headline-css').get(0);
console.log(a, typeof a)
但是控制台输出显示了实际内容,但它似乎是一个对象而不是字符串,我特别希望它是一个字符串。
查看演示 https://jsfiddle.net/collizo4sky/qh6dnmg9/
请帮忙
最佳答案
使用元素的outerHTML
属性
The
outerHTML
attribute of the elementDOM
interface gets the serialized HTML fragment describing the element including its descendants.
var a = jQuery('#swp-google-font-headline-css').get(0).outerHTML;
console.log(a,typeof a)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link rel="stylesheet" id="swp-google-font-headline-css" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro&ver=4.5.3" type="text/css" media="all">ffff
关于javascript - 通过 jQuery 获取带有 ID 的 <link> 元素的 HTML 主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38181726/