0

here is the problem and its really really killing me becauseit probably has a very simple solution. check out this jsfiddle

<body>
<p>
    this is the first paragraph and it will get the prepend without any problems at all
</p>
<span>
    this is a simple span element, and it will also get the prepend without any problems at all
</span>
<em>
    heres a em and this works as well

</em>
<p>
   here is another para and it works , BUT NOW LOOK AT THE NEXT LINE
    <br>BR STARTS HERE
        where is this prepend, ladies and gentlement,,NADA.
    </br>
</p>

$("*", document.body).each(function () {
$(this).prepend('<b><font color="red">  soccer  </font></b>');
});

you will find a simple set of HTML tags just a few randomly placed <p> and <span> tags. I have written a small jquery (2 lines function) that loops through all the tags in the body of the page and prepends a piece of html to each tag.

Unfortunately, the <br> tags do not seem to follow what all the other tags follo. It does not get the prepended HTML and I just can't figure it out.

koopajah
  • 23,792
  • 9
  • 78
  • 104
Algorini
  • 824
  • 1
  • 12
  • 19
  • 1
    tags are not meant to be used with a closing tag. You use it like:
    or
    to break to a new line.
    – Derek Feb 15 '13 at 15:49
  • Your markup is invalid. BR tags can not have content, so the problem you're describing is non existent! – adeneo Feb 15 '13 at 15:50
  • As @Derek said,
    is used alone. In XHTML, you must use it as
    . But it does not wrap content like most other tags. It operates alone.
    – Pete Feb 15 '13 at 15:50

2 Answers2

1

It's because a <br/> tag can't have any children elements or content https://developer.mozilla.org/en-US/docs/HTML/Element/br.. if you use .before()/.after() it will show up as a sibling as you wanted.

http://jsfiddle.net/Gmbpt/

wirey00
  • 33,517
  • 7
  • 54
  • 65
  • thanks wirey,, this is what i was looking for, i needed to add something to identify each and every node in the DOM, and i was stuck on BR,,, just a small follow up qustion -- is there any other tag/tags that behave like this , that i need to watch out for,, or will .before() cover them all. – Algorini Feb 15 '13 at 16:05
  • @user2076066 http://stackoverflow.com/a/7854998/1385672 - but before should cover each every tag since it's adding at the sibling level – wirey00 Feb 15 '13 at 16:18
0

If you read the specs (http://www.w3.org/TR/html5/text-level-semantics.html#the-br-element) you will see that the content model of br is empty.

This means that it can not have contents..

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317