<a href="example.html">Example Name</a>
我想獲得「實例名稱」
鏈接的名字,我知道我可以用正則表達式做到這一點,但我正在尋找一個簡單,更快速的方法。我最接近的是Jquery使用.attr("href")
屬性。我試着把.attr("title")
,但這不起作用,因爲我在技術上沒有一個標題。
<a href="example.html">Example Name</a>
我想獲得「實例名稱」
鏈接的名字,我知道我可以用正則表達式做到這一點,但我正在尋找一個簡單,更快速的方法。我最接近的是Jquery使用.attr("href")
屬性。我試着把.attr("title")
,但這不起作用,因爲我在技術上沒有一個標題。
.text()
試試這個
var t = $('a').text();
alert(t);
http://jsfiddle.net/jasongennaro/gZsbW/
當然,這個目標它遇到的第一個環節。如果你能將它掛到ID
,那更好。
例
<a href="example.html" id="linkName">Example Name</a>
然後
var t = $('#linkName').text();
您可以使用這樣的事情,這在常規的JavaScript的作品......
這樣做的優勢在於,它將提取文本諸如:
<a href="#">This is a <i>link</i> with <b>markup</b></a>
var getText = function(el) {
var ret;
var txt = [],i=0;
if (!el) {
ret = "";
} else if (el.nodeType === 3) {
// No problem if it's a text node
ret = el.nodeValue;
} else {
// If there is more to it, then let's gather it all.
while(el.childNodes[i]) {
txt[txt.length] = self.getText(el.childNodes[i]);
i++;
}
// return the array as a string
ret = txt.join("");
}
return ret;
};
嘗試var LinkName = document.links.text;
或者對於IE,您將需要var LinkName = document.links.innerText
爲什麼不只是html()爲jquery或innerHTML使用javascript? – thescientist
你也可以使用其中任何一種。 –