Using
$("a").attr("href", "http://www.google.com/")
will modify the href of all hyperlinks to point to Google. You probably want a somewhat more refined selector though. For instance, if you have a mix of link source (hyperlink) and link target (a.k.a. "anchor") anchor tags:
<a name="MyLinks"></a>
<a href="http://www.codeproject.com/">The CodeProject</a>
...Then you probably don't want to accidentally add href
attributes to them. For safety then, we can specify that our selector will only match <a>
tags with an existing href
attribute:
$("a[href]") //...
Of course, you'll probably have something more interesting in mind. If you want to match an anchor with a specific existing href
, you might use something like this:
$("a[href='http://www.google.com/']").attr('href', 'http://www.live.com/')
This will find links where the href
exactly matches the string http://www.google.com/
. A more involved task might be matching, then updating only part of the href
:
$("a[href^='http://stackoverflow.com']")
.each(function()
{
this.href = this.href.replace(/^http:\/\/beta\.stackoverflow\.com/,
"http://stackoverflow.com");
});
The first part selects only links where the href starts with http://stackoverflow.com
. Then, a function is defined that uses a simple regular expression to replace this part of the URL with a new one. Note the flexibility this gives you - any sort of modification to the link could be done here.
Update: I wrote a blog post detailing all the differences much better.
Firefox uses W3C standard Node::textContent
, but its behavior differs "slightly" from that of MSHTML's proprietary innerText
(copied by Opera as well, some time ago, among dozens of other MSHTML features).
First of all, textContent
whitespace representation is different from innerText
one. Second, and more importantly, textContent
includes all of SCRIPT tag contents, whereas innerText doesn't.
Just to make things more entertaining, Opera - besides implementing standard textContent
- decided to also add MSHTML's innerText
but changed it to act as textContent
- i.e. including SCRIPT contents (in fact, textContent
and innerText
in Opera seem to produce identical results, probably being just aliased to each other).
textContent
is part of Node
interface, whereas innerText
is part of HTMLElement
. This, for example, means that you can "retrieve" textContent
but not innerText
from text nodes:
var el = document.createElement('p');
var textNode = document.createTextNode('x');
el.textContent; // ""
el.innerText; // ""
textNode.textContent; // "x"
textNode.innerText; // undefined
Finally, Safari 2.x also has buggy innerText
implementation. In Safari, innerText
functions properly only if an element is
neither hidden (via style.display == "none"
) nor orphaned from the document. Otherwise, innerText
results in an empty string.
I was playing with textContent
abstraction (to work around these deficiencies), but it turned out to be rather complex.
You best bet is to first define your exact requirements and follow from there. It is often possible to simply strip tags off of innerHTML
of an element, rather than deal with all of the possible textContent
/innerText
deviations.
Another possibility, of course, is to walk the DOM tree and collect text nodes recursively.
Best Solution
just use
works in IE, Firefox and Chrome as far as I can tell.
see http://msdn.microsoft.com/en-us/library/aa767731(VS.85).aspx for more info