利用js來(lái)實(shí)現(xiàn)縮略語(yǔ)列表、文獻(xiàn)來(lái)源鏈接和快捷鍵列表
1 縮略語(yǔ)列表問(wèn)題出發(fā)點(diǎn):一段包含大量縮略語(yǔ)的文本,例如:
<p> The <abbr title="World Wide Web Consortium">W3C</abbr> defines the <abbr title="Document Object Model">DOM</abbr> as: </p> <blockquote cite="http://www.w3.org/DOM/"> <p> A platform- and language-neutral interface that will allow programs and scripts to dynamically access and update the content, structure and style of documents. </p> </blockquote> <p> It is an <abbr title="Application Programming Interface">API</abbr> that can be used to navigate <abbr title="HyperText Markup Language">HTML</abbr> and <abbr title="eXtensible Markup Language">XML</abbr> documents. </p>
縮略語(yǔ)標(biāo)簽的title屬性在瀏覽器里是隱藏的,不同瀏覽器對(duì)縮略語(yǔ)的默認(rèn)呈現(xiàn)樣式是不同的,那么這多少都會(huì)影響用戶的體驗(yàn)。一個(gè)比較好的解決方法是,將這些縮列語(yǔ)通過(guò)列表的方式展示出來(lái)。如:
<dl> <dt>縮略語(yǔ)標(biāo)題/abbr.lastChild.nodeValue</dt> <dd>縮略語(yǔ)定義描述/abbr.getAttribute</dd> ...... </dl>
用DOM來(lái)創(chuàng)建這個(gè)列表(即用js來(lái)動(dòng)態(tài)創(chuàng)建html標(biāo)記,常見(jiàn)的方法見(jiàn) 詳解js的事件處理函數(shù)和動(dòng)態(tài)創(chuàng)建html標(biāo)記方法),大致過(guò)程如下:
(1)遍歷abbr
(2)保存abbr的title屬性和文本
(3)創(chuàng)建定義列表元素dl
(4)創(chuàng)建定義標(biāo)題元素dt
(5)將abbr的文本插入到這個(gè)dt元素
(6)創(chuàng)建定義描述元素dd
(7)將abbr的title屬性插入到這個(gè)dd元素
(9)追加以上創(chuàng)建的各元素
代碼如下:
function displayAbbreviations() {
//注釋1:注意這里沒(méi)有對(duì)DOM方法做兼容性檢查
var abbreviations = document.getElementsByTagName("abbr");
var defs = new Array();//注釋2:用數(shù)組的鍵值對(duì)來(lái)保存abbr的title屬性和文本
//loop through the abbr list
for (var i=0; i<abbreviations.length; i++) {
var current_abbr = abbrevaitons[i];
//注釋3:if (current_abbr.childNodes.length < 1) continue;
var defination = current_abbr.getAttributes("title");
var key = current_abbr.lastChild.nodeValue;
defs[key] = defination;
}
var dlist = document.createElement("dl");
//loop through the defs
for (key in defs) {
var defination = defs[key];
var dtitle = document.createElement("dt");
var dtitle_text = document.createTextNode(key);
dtitle.appendChild(dtitle_text);
var ddesc = document.createElement("dd");
var ddesc_text = document.createTextNode(defination);
ddesc.appendChild(ddesc_text);
dlist.appendChild(dtitle);
dlist.appendChild(ddesc);
}
//注釋4:if (dlist.childNodes.length < 1) return false;
var header = document.createElement("h2");
var header_text = document.createElement("Abbreviations");
header.appendChild(header_text);
//注釋5:下面兩行用到了HTML-DOM屬性:document.body,也可以用DOM core的 document.getElementsByTagName("body")[0]方法;
document.body.appendChild(header);
document.body.appendChild(dlist);
}
displayAbbreviations有很多改進(jìn)的余地,比如注釋1提到的DOM方法兼容性檢查問(wèn)題。還有就是IE6不支持<abbr>的問(wèn)題,這個(gè)問(wèn)題可以通過(guò)增加注釋3和注釋5中的語(yǔ)句來(lái)解決。注釋3解決了IE6及之前的版本的IE在統(tǒng)計(jì)abbr元素節(jié)點(diǎn)時(shí)總是返回0的問(wèn)題,注釋5則解決了瀏覽器不支持abbr元素而出現(xiàn)js報(bào)錯(cuò)的問(wèn)題。
2 動(dòng)態(tài)創(chuàng)建文獻(xiàn)來(lái)源鏈接的實(shí)現(xiàn)方法和縮列語(yǔ)列表的方法大致相同
<blockquote> 標(biāo)簽定義塊引用,它有一個(gè)可選的cite屬性,這個(gè)屬性規(guī)定了引用的來(lái)源。該屬性的值是一個(gè)包含在引號(hào)中并指向某個(gè)網(wǎng)頁(yè)的 URL地址。這個(gè)屬性很有用,它可以將文獻(xiàn)資料和相關(guān)網(wǎng)頁(yè)鏈接起來(lái)。但主流瀏覽器均不支持 cite 屬性,一般都會(huì)將它忽略,用戶也看不到。
將1中的html代碼中 <blockquote> 的cite屬性以鏈接的形式顯示出來(lái),代碼如下:
function displayCitations() {
//兼容性檢查
if (!document.getElementsByTagName || !document.createElement
|| !document.createTextNode) return false;
//獲取所有的blockquote元素
var quotes = document.getElementsByTagName("blockquote");
//1 遍歷blockquote元素
for (var i=0; i<quotes.length; i++) {
// 檢查是否存在cite屬性
if (!quotes[i].getAttribute("cite")) continue;
// 2 提取cite屬性的值
var url = quotes[i].getAttribute("cite");
// 獲取blockquote包含的所有元素節(jié)點(diǎn),注意是元素節(jié)點(diǎn),這樣就把文本節(jié)點(diǎn)排除掉了
var quoteChildren = quotes[i].getElementsByTagName("*");
// 判斷元素是否為空
if (quoteChildren.length < 1) continue;
var elem = quoteChildren[quoteChildren.length - 1];//獲取最后一個(gè)元素節(jié)點(diǎn)
var link = document.createElement("a");//3 創(chuàng)建鏈接節(jié)點(diǎn)
var link_text = document.createTextNode("source");
link.appendChild(link_text);
link.setAttribute("href", url);//4 給鏈接節(jié)點(diǎn)的href屬性賦值
var superscript = document.createElement("sup");
superscript.appendChild(link);
elem.appendChild(superscript);//5 追加節(jié)點(diǎn)到<blockquote>包含節(jié)點(diǎn)的末尾
}
}
3 accesskey 屬性可以將<a>鏈接與鍵盤的特定按鍵關(guān)聯(lián)在一起,如:<a href="index.html" accesskey="1">Home</a>,不過(guò)好像不是所有的瀏覽器都支持這個(gè)屬性,比如Opera。
將下面html代碼中的accesskey屬性像上面縮列語(yǔ)以列表的形式展示出來(lái)。
<ul id="navigation"> <li><a href="index.html" accesskey="1">Home</a></li> <li><a href="search.html" accesskey="4">Search</a></li> <li><a href="contact.html" accesskey="0">Contact</a></li> </ul>
代碼如下:
function displayAccesskeys() {
if (!document.getElementsByTagName || !document.createElement || !document.createTextNode) return false;
// get all the links in the document
var links = document.getElementsByTagName("a");
// create an array to store the accesskeys
var akeys = new Array();
// loop through the links
for (var i=0; i<links.length; i++) {
var current_link = links[i];
// if there is no accesskey attribute, continue the loop
if (current_link.getAttribute("accesskey") == null) continue;
// get the value of the accesskey
var key = current_link.getAttribute("accesskey");
// get the value of the link text
var text = current_link.lastChild.nodeValue;
// add them to the array
akeys[key] = text;
}
// create the list
var list = document.createElement("ul");
// loop through the accesskeys
for (key in akeys) {
var text = akeys[key];
// create the string to put in the list item
var str = key + " : "+text;
// create the list item
var item = document.createElement("li");
var item_text = document.createTextNode(str);
item.appendChild(item_text);
// add the list item to the list
list.appendChild(item);
}
// create a headline
var header = document.createElement("h3");
var header_text = document.createTextNode("Accesskeys");
header.appendChild(header_text);
// add the headline to the body
document.body.appendChild(header);
// add the list to the body
document.body.appendChild(list);
}
addLoadEvent(displayAccesskeys);
最后實(shí)現(xiàn)的網(wǎng)頁(yè)效果如下:

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
- 任意Json轉(zhuǎn)成無(wú)序列表的方法示例
- Vue.js Ajax動(dòng)態(tài)參數(shù)與列表顯示實(shí)現(xiàn)方法
- Vuejs第一篇之入門教程詳解(單向綁定、雙向綁定、列表渲染、響應(yīng)函數(shù))
- AngularJS實(shí)現(xiàn)數(shù)據(jù)列表的增加、刪除和上移下移等功能實(shí)例
- 將List對(duì)象列表轉(zhuǎn)換成JSON格式的類實(shí)現(xiàn)方法
- VUEJS實(shí)戰(zhàn)之構(gòu)建基礎(chǔ)并渲染出列表(1)
- 基于BootStrap Metronic開(kāi)發(fā)框架經(jīng)驗(yàn)小結(jié)【二】列表分頁(yè)處理和插件JSTree的使用
- javascript實(shí)現(xiàn)列表切換效果
- JSON的String字符串與Java的List列表對(duì)象的相互轉(zhuǎn)換
- jsp頁(yè)面 列表 展示 ajax異步實(shí)現(xiàn)方法
- JS動(dòng)態(tài)的把左邊列表添加到右邊的實(shí)現(xiàn)代碼(可上下移動(dòng))
相關(guān)文章
javascript parseUrl函數(shù)(來(lái)自國(guó)外的獲取網(wǎng)址url參數(shù))
在外國(guó)一博客看到一個(gè)很好的函數(shù),獲取網(wǎng)址url等地址參數(shù)。非常不錯(cuò),值得參考與收藏。2010-06-06
深入理解javascript作用域第二篇之詞法作用域和動(dòng)態(tài)作用域
這篇文章主要介紹了javascript作用域第二篇之詞法作用域和動(dòng)態(tài)作用域的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友可以參考下2016-07-07
js簡(jiǎn)單的點(diǎn)擊返回頂部效果實(shí)現(xiàn)方法
這篇文章主要介紹了js簡(jiǎn)單的點(diǎn)擊返回頂部效果實(shí)現(xiàn)方法,實(shí)例分析了實(shí)現(xiàn)返回頂部效果的相關(guān)要點(diǎn)與實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-04-04
Js判斷兩個(gè)數(shù)組是否相等的幾種常見(jiàn)場(chǎng)景
無(wú)論是在開(kāi)發(fā)中還是面試時(shí),在js中判斷變量是否相等,都是一個(gè)比較常見(jiàn)的問(wèn)題,這篇文章主要給大家介紹了關(guān)于Js判斷兩個(gè)數(shù)組是否相等的幾種常見(jiàn)場(chǎng)景,需要的朋友可以參考下2024-07-07
JS數(shù)組屬性去重并校驗(yàn)重復(fù)數(shù)據(jù)
這篇文章主要介紹了JS數(shù)組屬性去重并校驗(yàn)重復(fù)數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
sass 中使用 /deep/ 修改 elementUI 組件樣式報(bào)錯(cuò)
這篇文章主要介紹了sass 中使用 /deep/ 修改 elementUI 組件樣式報(bào)錯(cuò)的解決方案,嘗試用 ::v-deep 替換 /deep/ ,成功解決了問(wèn)題,感興趣的朋友跟隨小編一起看看吧2024-02-02

