获取链接标签的内容与JavaScript - 不是CSS

问题描述:

假设我有获取链接标签的内容与JavaScript - 不是CSS

<LINK rel="Index" href="index.html"> 
<LINK rel="Next" href="Chapter3.html"> 
<LINK rel="Prev" href="Chapter1.html"> 

有谁知道这些都是通过JavaScript的DOM访问(从W3网站取试样)?

我想知道如果我在HTML文档中有这样的链接标签,它们是否像主文档一样被读取并添加到DOM,以及我是否也可以访问它们的DOM。

+0

发送一个Ajax请求检索他们的内容? – 2012-07-29 10:11:04

+0

不确定您是否想要获取“链接”标记或该链接标记引用的文件内容。 – devundef 2012-07-29 10:13:48

+0

我想知道如果我在HTML文档中有这样的链接标签,它们是否像主文档一样被读取并添加到DOM,以及我是否也可以访问它们的DOM。 – sproketboy 2012-07-29 10:16:56

试试这个 - http://jsfiddle.net/TpTsJ/(第2是的jsfiddle链接 - 你不会有他们在您的网页上)

var links = document.getElementsByTagName("link"); 
for (var i = 0; i < links.length; i++) { 
    alert(links[i].getAttribute("rel") + ' : ' + links[i].getAttribute("href"));   
}​ 
+0

是的,我可以这样做,但我想知道我是否可以访问href的实际DOM。我知道我可以通过ajax调用,但也许浏览器已经这样做了。 – sproketboy 2012-07-29 10:24:26

+1

没有。只有ajax的方式。 – 2012-07-29 22:21:57

我有这样的代码:

<script type="text/javascript"> 
var your_url = 'http://www.example.com'; 
</script> 

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" ></script> 
<script type="text/javascript"> 
// jquery.xdomainajax.js ------ from padolsey 

jQuery.ajax = (function(_ajax){ 

    var protocol = location.protocol, 
     hostname = location.hostname, 
     exRegex = RegExp(protocol + '//' + hostname), 
     YQL = 'http' + (/^https/.test(protocol)?'s':'') + '://query.yahooapis.com/v1/public/yql?callback=?', 
     query = 'select * from html where url="{URL}" and xpath="*"'; 

    function isExternal(url) { 
     return !exRegex.test(url) && /:\/\//.test(url); 
    } 

    return function(o) { 

     var url = o.url; 

     if (/get/i.test(o.type) && !/json/i.test(o.dataType) && isExternal(url)) { 

      // Manipulate options so that JSONP-x request is made to YQL 

      o.url = YQL; 
      o.dataType = 'json'; 

      o.data = { 
       q: query.replace(
        '{URL}', 
        url + (o.data ? 
         (/\?/.test(url) ? '&' : '?') + jQuery.param(o.data) 
        : '') 
       ), 
       format: 'xml' 
      }; 

      // Since it's a JSONP request 
      // complete === success 
      if (!o.success && o.complete) { 
       o.success = o.complete; 
       delete o.complete; 
      } 

      o.success = (function(_success){ 
       return function(data) { 

        if (_success) { 
         // Fake XHR callback. 
         _success.call(this, { 
          responseText: data.results[0] 
           // YQL screws with <script>s 
           // Get rid of them 
           .replace(/<script[^>]+?\/>|<script(.|\s)*?\/script>/gi, '') 
         }, 'success'); 
        } 

       }; 
      })(o.success); 

     } 

     return _ajax.apply(this, arguments); 

    }; 

})(jQuery.ajax); 



$.ajax({ 
    url: your_url, 
    type: 'GET', 
    success: function(res) { 
     var text = res.responseText; 
     // then you can manipulate your text as you wish 
     alert(text); 
    } 
}); 

</script> 
+0

虽然这似乎不适用于所有网址。我在尝试使用url = http://www.andy-howard.com/blog.htm时遇到错误,为什么? – 2013-09-26 08:01:09

+0

而我觉得这是有限的。它由于某种原因停止工作。 – 2016-01-21 23:18:11

+0

对我不起作用 – 2017-03-20 12:47:28