html - Javascript: How to tell if a span is the first thing in a paragraph? -
i objective c programmer, , need javascript in latest project. 1 week ago had never worked on javascript. have spent few hours on particular problem , can't figure out. know virtually nothing javascript, apologise in advance if make basic errors (very likely).
basically have html page. there number of custom span tags in page. need function tells me if particular span tag first thing (not first span) in paragraph. e.g. if html looks this:
<p><span class="myclass">the</span> quick brown fox jumps on lazy <span class="myclass">dog.</span></p>
and use
var spanlist = document.getelementsbyclassname("myclass");
i need function return true
isfirstinparagraph(spanlist[0]); //ie. word "the"
and false for
isfirstinparagraph(spanlist[1]); // ie. word "dog."
and in following case, both spans return false, since neither first thing in paragraph.:
<p>the <span class="myclass">quick</span> brown fox jumps on lazy <span class="myclass">dog.</span></p>
i love attach semi working code, nothing have done has come close. seems me 1 way approach find range offset of span relative paragraph, , see if zero. have spent hours reading through documentation , can't figure out. can't think because lack of understanding of general concepts , terms in javascript/html.
things might relevant:
- paragraphs may or may not have first word inset.
- not myclass spans in paragraph, in heading example.
- the myclass span in italics or bold etc..
- this needs javascript not query.
- it inside native ios app, in uiwebview. assume safari compatible work.
thanks in advance
get list of spans that:
spanlist = document.getelementsbytagname("span"); // spans in document
then function should that:
function isfirstinparagraph(span) { var p = span.parentnode; if (p.nodename === "p") // span contained within 'p' { if (p.firstchild === span) return true; else return false; } else { return false; } }
Comments
Post a Comment