2

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".

3
  • This is not a good title for your question. The title should be a summary of the question so that people can effectively skim for and search for relevant topics. Commented Sep 17, 2008 at 5:12
  • If you could include the CSS styles that are being applied to span it would help us determine how to best answer the question.
    – Prestaul
    Commented Sep 17, 2008 at 5:29
  • The problem is I don't know the CSS style until runtime, because I'm writing a javascript program to run on many different web pages.
    – poor boy
    Commented Sep 17, 2008 at 5:35

5 Answers 5

1

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;
  ...
}
2
  • 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
    Commented Sep 17, 2008 at 6:06
  • 'static' is the default position value for most elements. See: w3schools.com/Css/pr_class_position.asp
    – Prestaul
    Commented Sep 17, 2008 at 6:08
1

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.

4
  • But I don't know the whether the outter element is a <p> or a span.
    – poor boy
    Commented Sep 17, 2008 at 5:25
  • 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. Commented Sep 17, 2008 at 5:40
  • 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
    Commented Sep 17, 2008 at 6:07
  • Nested paragraph tags? That's awful.
    – Evil Andy
    Commented Sep 17, 2008 at 11:57
1

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>
3
  • 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
    Commented Sep 17, 2008 at 5:31
  • @poor_boy: Your text does not have margin or padding. It cannot because it is simply text...
    – Prestaul
    Commented Sep 17, 2008 at 5:33
  • Oh sorry, I mean the style of the outter text, it's determined by the element in which the text is.
    – poor boy
    Commented Sep 17, 2008 at 5:43
0

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.

1
  • Thanks for replay. Because I want to get the position of the element I just inserted.
    – poor boy
    Commented Sep 17, 2008 at 5:21
0

Include the class definition that's defined in CSS on your JavaScript version of the <span> tag as well.

<span class="class_defined_in_css">

(where this <span> tag would be part of your JavaScript code.)

1
  • This won't help if the original style is applied to all span tags.
    – poor boy
    Commented Sep 17, 2008 at 5:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.