svintuss wrote:
Hi. I'm trying to create a widget for Tiger, which should use a
web-dictionary. I want the widget to display translation within its
window, so it has to copy all the data within the tags <span
class="lingvo-article"> </span>.
Is there a method like document.getElementById to get the class
contents?
Or it's only possible to search for opening and ending tags, then copy
the text inside?
I haven't written any Dashboard widgets, perhaps there's a better forum
for that. There's some good stuff here:
<URL:http://developer.apple.com/macosx/dashboard.html>
It seems to be pretty much the same as typical web stuff, so you should
be able to get the following working.
There is no 'getElementsByClassName' method, though it is asked for
reasonably often. You have to sift through the document looking for
matches. If you are only looking for span elements, then:
var docBody = document.body || document.documentElement;
if (!docBody || !docBody.getElementsByTagName) return;
var el;
var spans = docBody.getElementsByTagName('span');
var classSpan = [];
var classMatch = /\blingvo-article\b/;
for (var i=0, len=spans.length; i<len; ++i){
el = spans[i];
if (el.className && classMatch.test(el.className)){
// el is a 'lingvo-article' span,
// do something with it
}
}
If you are looking for all elements and not just spans, you'll need to
replace:
var spans = docBody.getElementsByTagName('span');
with something like:
var allElems = document.getElementsByTagName("*");
Presumably this will only ever run on Mac OS X 10.4 or higher, so
feature detection and fall-back is not really required.
I've recently posted stuff on getting the text content of a cell, search
the archive for 'textContent innerText' and sort by date. If Tiger
supports textContent (it's in DOM 3 so maybe not but Firefox has it),
you can get the element's text content quite simply:
var theContent = el.textContent;
--
Rob