从<link>获取值

时间:2019-10-24 06:13:05

标签: javascript

朋友,我想获得,或者只是知道它是否存在,有人有想法吗?

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
    <link rel="stylesheet" type="text/css" href="stacks.css?v=f956aeec157c" >
    <link rel="stylesheet" type="text/css" href="primary.css?v=f68ca0e24c3b" >

</head>

<body>

 
    <link extension-installed="0318648714777">


<p>Click the button to find out how many <link> elements.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
  var x = document.getElementsByTagName("link");

  document.getElementById("demo").innerHTML = x.length;
}
</script>
</pre>

不幸的是,它计算了所有页面

4 个答案:

答案 0 :(得分:1)

querySelector与选择器字符串body > link配合使用,可直接在体内找到<link>

function myFunction() {
  const link = document.querySelector('body > link');
  console.log(link);
  console.log(link.getAttribute('extension-installed'));
}
</style>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="stacks.css?v=f956aeec157c">
<link rel="stylesheet" type="text/css" href="primary.css?v=f68ca0e24c3b">

<style>
<link extension-installed="0318648714777">


<p>Click the button to find out how many
  <link> elements.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

如果它不是身体的直接子代,请改用body link,以获取身体内部任何位置的链接。

答案 1 :(得分:0)

您可以使用querySelectorAll("link")。它将给出包含所有链接元素的节点列表

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
    <link rel="stylesheet" type="text/css" href="stacks.css?v=f956aeec157c" >
    <link rel="stylesheet" type="text/css" href="primary.css?v=f68ca0e24c3b" >

</head>

<body>


    <link extension-installed="0318648714777">


<p>Click the button to find out how many link elements.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
  var x = document.querySelectorAll("link");

  document.getElementById("demo").innerHTML = x.length;
}
</script>
</pre>

答案 2 :(得分:0)

如果所有链接都具有公共属性,则可以尝试通过属性 extension-installed 来获取。.document.querySelectorAll('[extension-installed]');

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>Untitled Document</title>
  <link rel="stylesheet" type="text/css" href="stacks.css?v=f956aeec157c">
  <link rel="stylesheet" type="text/css" href="primary.css?v=f68ca0e24c3b">

</head>

<body>


  <link extension-installed="0318648714777">


  <p>Click the button to find out how many
    <link> elements.</p>

  <button onclick="myFunction()">Try it</button>

  <p id="demo"></p>

  <script>
    function myFunction() {
      var x = document.querySelectorAll('[extension-installed]');

      document.getElementById("demo").innerHTML = x.length;
    }
  </script>
  </pre>

答案 3 :(得分:0)

在搜索带有标签的元素时:

  document.getElementsByTagName("link");

那里实际上是四个:

<link rel="stylesheet" type="text/css" href="stacks.css?v=f956aeec157c"></link>,
<link rel="stylesheet" type="text/css" href="primary.css?v=f68ca0e24c3b"></link>,
<link extension-installed="0318648714777"></link>,
<link></link>

如果您需要特定的链接,请尝试使用带有ID的ID,然后按ID选择:

<link extension-installed="0318648714777" id="myLink"></link>

var x = document.getElementsById("myLink");

或者只是尝试查找具有属性extension-installed的链接

var x = document.querySelectorAll('[extension-installed]');