tags:

views:

853

answers:

5

I want to use javascript to insert some elements into the current page. Such as this is the original document: <p>Hello world!</p>

Now I want to insert an element in to the text so that it will become:

<p>Hello <span id=span1>new</span> world!</p>

I need the span tag because I want to handle it later.Show or hide. But now problem comes out, if the original page has already defined a strange CSS style on all <span> tags, the "new" I just inserted will not appear to be the same as "Hello" and "world". How can I avoid this? I want the "new" be exactly the same as the "Hello" and "world".

Thanks for your help~

A: 

Include the class definition that's defined in CSS on your javascript version of the tag as well.

(where this tag would be part of your javascript code.

Sev
This won't help if the original style is applied to all span tags.
poor boy
A: 

Why not give the paragraph an id and then use Javascript to add the word, or remove it, if necessary? Surely it will retain the same formatting as the paragraph when you insert the word "new", or change the contents of the paragraph entirely.

dreamlax
Thanks for replay.Because I want to get the position of the element I just inserted.
poor boy
+1  A: 

Well, I don't know how married you are to using a <span> tag, but why not do this?

<p style="display: inline">Hello <p id="myIdValue" style="display: inline">new</p> World</p>

That way the inserted html retains the same styling as the outer, and you can still have a handle to it, etc. Granted, you will have to add the inline CSS style, but it would work.

Jason Bunting
But I don't know the whether the outter element is a <p> or a span.
poor boy
well, that is easy enough to determine with JavaScript - once you have a reference to the element you are inserting into, you check the tagName property and it will tell you - then you can simply insert the same.
Jason Bunting
What if it's "body"?
poor boy
There is still no guarantee that styles will be the same. What if the developer used body>p as a selector or has already applied styles to Ps nested in Ps?
Prestaul
True, very true.
Jason Bunting
Nested paragraph tags? That's awful.
Evil Andy
Also true - I wouldn't do it myself, but some people don't have such qualms...
Jason Bunting
+1  A: 

The only way to do this is to either modify the other spans to include a class name and only apply the styles to spans with that class, or override the styles set for all spans for your new span.

So if you've done:

span {
  display: block;
  margin: 10px;
  padding: 10px;
}

You could override with:

<span style="display: inline; margin: 0; padding: 0;">New Span</span>
Prestaul
But I don't know the margin and padding of the outter text, I want the element I inserted to appear like the outter text.
poor boy
@poor_boy: Your text does not have margin or padding. It cannot because it is simply text...
Prestaul
Oh sorry, I mean the style of the outter text, it's determined by the element in which the text is.
poor boy
+1  A: 

Simply override any span styles. Set layout properties back to browser defaults and set formating to inherit from the parent:

span#yourSpan {
  /* defaults */
  position: static;
  display: inline;
  margin: 0;
  padding: 0;
  background: transparent;
  border: none;

  /* inherit from parent node */
  font: inherit;
  color: inherit;
  text-decoration: inherit;
  line-height: inherit;
  letter-spacing: inherit;
  text-transform: inherit;
  white-space: inherit;
  word-spacing: inherit;
}

This should be sufficient, although you may need to add !important if you are not using an id:

<span class="hello-node">hello</span>

span.hello-node {
  /* defaults */
  position: static !important;
  display: inline !important;
  ...
}
Prestaul
You are right, the margin and padding should be always 0 for text.Thanks a lot for helping.BTW, what's the position "static"?
poor boy
'static' is the default position value for most elements. See: http://www.w3schools.com/Css/pr_class_position.asp
Prestaul
Thanks,this is the perfect solution :D
poor boy