使用 javascript 获取链接标签的内容 - 而不是 CSS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11708387/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:48:44  来源:igfitidea点击:

Get contents of link tag with javascript - not CSS

javascriptdomlink-tag

提问by sproketboy

Assuming I have

假设我有

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

(taken from w3 web site sample)

(取自 w3 网站示例)

Does anyone know if these are accessible through the JavaScript DOM?

有谁知道这些是否可以通过 JavaScript DOM 访问?

I want to know If I have link tags like this in the HTML document whether they are read like the main document and added to the DOM and if I can access their DOMs as well.

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

回答by otaxige_aol

I have this code:

我有这个代码:

<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>

回答by Zoltan Toth

Try this - http://jsfiddle.net/TpTsJ/(the first 2 are the JSFiddle links - you won't have them on your page)

试试这个 - http://jsfiddle.net/TpTsJ/(前两个是 JSFiddle 链接 - 你的页面上不会有它们)

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