获取链接标签的内容与JavaScript - 不是CSS [英] Get contents of link tag with javascript - not CSS
本文介绍了获取链接标签的内容与JavaScript - 不是CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有
< LINK rel =Indexhref =index.html>
< LINK rel =Nexthref =Chapter3.html>
< LINK rel =Prevhref =Chapter1.html>
(取自w3网站样本)
<有没有人知道这些可以通过JavaScript DOM访问?
我想知道如果我在HTML文档中有这样的链接标签,无论它们是否像主要文件,并添加到DOM中,并且我可以访问其DOM。
解决方案
我有这个代码:
< script type =text / javascript>
var your_url ='http://www.example.com';
< / script>
< script type =text / javascriptsrc =// ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js>< /脚本>
< script type =text / javascript>
// jquery.xdomainajax.js ------ from padolsey
jQuery.ajax =(function(_ajax){
var protocol = location。协议,
hostname = location.hostname,
exRegex = RegExp(protocol +'//'+ hostname),
YQL ='http'+(/^https/.test(protocol) ?''':'')+'://query.yahooapis.com/v1/public/yql?callback =?',
query ='select * from html where url ={URL}and xpath =*';
函数isExternal(url){
return!exRegex.test(url)&& / /\/\\////.test );
}
返回函数(o){
var url = o.url;
if(/ get / i .test(o.type)&!/json/i.test(o.dataType)&& isExternal(url)){
//操作选项,使JSONP-x请求YQL
o.url = YQL;
o.dataType ='json';
o.data = {
q:查询.replace(
'{URL}',
url +(o.data?
(/\?/.test(url)?'&':'?')+ jQuery.param(o.data)
:'')
),
格式:'xml'
};
//由于它是一个JSONP请求
//完成===成功
如果(!o.success&& o.complete){
o .success = o.complete;
删除o.complete;
}
o.success =(function(_success){
return function(data){
if(_success){
// Fake XHR callback
_success.call(this,{
responseText:data.results [0]
// YQL screw with< script> s
// Get摆脱他们
.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;
//那么你可以按需要操纵你的文本
alert(text);
}
});
< / script>
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)
Does anyone know if these are accessible through the 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.
解决方案
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>
这篇关于获取链接标签的内容与JavaScript - 不是CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文