国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > HTML > 正文

HTML網(wǎng)頁(yè)中的URL表示方式

2020-03-24 16:36:20
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
相對(duì)URL: example.php
demo/example.php
./example.php
../../example.php
/example.php絕對(duì)URL: http://dancewithnet.com/example.php
http://dancewithnet.com:80/example.php
http://dancewithnet.com/example.php同時(shí)HTML中有大量的元素屬性值為URL,一般利用JavaScript獲取這些URL屬性值有兩種方法: a href= example.php id= example-a 此時(shí)頁(yè)面絕對(duì)URL是http://dancewithnet.com/ /a
script
var oA = document.getElementById('example-a');
oA.href == 'http://dancewithnet.com/example.php';
oA.getAttribute('href') == 'example.php';
/script 我們希望通過(guò)直接訪問(wèn)屬性的方式得到完整絕對(duì)URL,通過(guò)getAttribute方法得到其原始的屬性值,實(shí)際上這是一個(gè)比較理想的結(jié)果,在所有的A級(jí)瀏覽器中,能順利得到這個(gè)結(jié)果的只有Firefox和IE8,其他瀏覽器都或多或少特殊情況,具體哪些元素的屬性存在什么樣的情況請(qǐng)看 Dealing with unqualified HREF html' target='_blank'>values》中的解決方案太過(guò)于復(fù)雜,這里提供一種相對(duì)簡(jiǎn)單的解決方案,如果不考慮區(qū)別瀏覽器代碼會(huì)非常簡(jiǎn)單: form action= example.php id= example-form
此時(shí)頁(yè)面絕對(duì)URL是http://dancewithnet.com/ /form
script
var oForm = document.getElementById('example-form');//IE6、IE7、Safari、Chrome、Opera
oForm.action == 'example.php';
oA.getAttribute('action') == 'example.php';//獲取絕對(duì)URL的通用解決方案
getQualifyURL(oForm,'action') == 'http://dancewithnet.com/example.php';getQualifyURL = function(oEl,sAttr){
var sUrl = oEl[sAttr],
oD,
bDo = false;
//是否是IE8之前版本
//http://www.thespanner.co.uk/2009/01/29/detecting-browsers-javascript-hacks/
//http://msdn.microsoft.com/en-us/library/7kx09ct1%28VS.80%29.aspx
/*@cc_on
try{
bDo = @_jscript_version 5.8 ?true : @false;
}catch(e){
bDo = false;
}
@*/
//如果是Safari、Chrome和Opera
if(/a/.__proto__=='//' || /source/.test((/a/.toString+''))
|| /^function /(/.test([].sort)){
bDo = true;
}
if(bDo){
oD = document.createElement('div');
/*
//DOM 操作得到的結(jié)果不會(huì)改變
var oA = document.createElement('a');
oA.href = oEl[sAttr];
oD.appendChild(oA);
*/
oD.innerHTML = [' a href= ',sUrl,' /a '].join('');
sUrl = oD.firstChild.href;
}
return sUrl;
}
/script 在IE6和IE7這兩個(gè)史前的瀏覽器身上還有一些更有意思的事情,兩種方法在HTML元素A、AREA和IMG獲取的屬性值都是絕對(duì)URL,幸好 微軟為getAttribute提供了第二個(gè)參數(shù) 可以解決這個(gè)問(wèn)題,同時(shí)還可以對(duì)IFEAM和LINK元素解決前面提到的兩種方法都返回原始屬性的問(wèn)題: link href= ../../example.css id= example-link
a href= example.php id= example-a 此時(shí)頁(yè)面絕對(duì)URL是http://dancewithnet.com/ /a script
var oA = document.getElementById('example-a'),
oLink = document.getElementById('example-a');oA.href == 'http://dancewithnet.com/example.php';
oA.getAttribute('href') == 'http://dancewithnet.com/example.php';
oA.getAttribute('href',2) == 'example.php';oLink.href == 'example.php';
oLink.getAttribute('href') == 'example.php';
oLink.getAttribute('href',4) == 'http://dancewithnet.com/example.php';
/scripthtml教程

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 盐源县| 韶山市| 梁山县| 河池市| 陵水| 临江市| 泰州市| 西城区| 靖边县| 沂南县| 凤庆县| 特克斯县| 桦甸市| 建平县| 阿巴嘎旗| 库伦旗| 河曲县| 吴川市| 当涂县| 乌鲁木齐县| 安徽省| 黔江区| 乐亭县| 齐齐哈尔市| 延长县| 文安县| 兰西县| 进贤县| 济阳县| 德庆县| 滨州市| 阳新县| 开封县| 维西| 吉木萨尔县| 浙江省| 林口县| 寻乌县| 扎兰屯市| 伊宁市| 时尚|