473,408 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Addressing pseudo-element first-letter in Javascript

Hello,

I have a stylesheet that sets p:first-letter to a certain
size and colour. I was playing around with Javascript to change
paragraph stylesheets using an array like this:

var paras = document.all.tags("P");

Although I can change the paragraph colours using this
array, the first letter of all the paragraphs remain the same
colour. How do I refer to an array of the pseudo-element
p:first-letter in the same way as the p tag?

Thanks!

Fungii
Jul 23 '05 #1
6 3957


Fungii wrote:

I have a stylesheet that sets p:first-letter to a certain
size and colour. I was playing around with Javascript to change
paragraph stylesheets using an array like this:

var paras = document.all.tags("P");
That doesn't change any stylesheets at all, you would need to use
document.styleSheets
to change style sheets.
Although I can change the paragraph colours using this
array, the first letter of all the paragraphs remain the same
colour. How do I refer to an array of the pseudo-element
p:first-letter in the same way as the p tag?


I don't think the DOM provides access to pseudo elements, you can
however (in IE/Win using
document.styleSheets[index].rules[index]
and in Mozilla using
document.styleSheets[index].cssRules[index]
) access the rules in the style sheets thus if you already have a rule e.g.
p:first-letter { font-weight: bold; }
then you could change/add style declarations in that rule, once you have
the rule e.g.
rule.style.color = 'yellow';
You can also add new rules if needed though the API in IE is different
to the API in Mozilla (which follows the W3C DOM standard:
<http://www.w3.org/TR/DOM-Level-2-Style/>
).
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
Martin Honnen <ma*******@yahoo.de> wrote in
news:41***********************@newsread4.arcor-online.net:

Fungii wrote:

I have a stylesheet that sets p:first-letter to a
certain
size and colour. I was playing around with Javascript to
change paragraph stylesheets using an array like this:

var paras = document.all.tags("P");
That doesn't change any stylesheets at all, you would need
to use
document.styleSheets
to change style sheets.


Yes I know, I was just pointing out how I created an array that
I can then use to modify paragraph styles like this:

paras(i).style.color = "#ffffff";
Although I can change the paragraph colours using
this
array, the first letter of all the paragraphs remain the
same colour. How do I refer to an array of the
pseudo-element p:first-letter in the same way as the p
tag?


I don't think the DOM provides access to pseudo elements,


Bogus, you would think if they are "there" you would be able to
get to them somehow.
you can however (in IE/Win using
document.styleSheets[index].rules[index]
and in Mozilla using
document.styleSheets[index].cssRules[index]
) access the rules in the style sheets thus if you already
have a rule e.g.
p:first-letter { font-weight: bold; }
then you could change/add style declarations in that rule,
once you have the rule e.g.
rule.style.color = 'yellow';
Ok, I'll look into that method. How would I get the rule index
for p:first-letter? Say I have one stylesheet in my page and the
first entry in that stylesheet is the "p:first-letter" entry.
Would that mean "document.styleSheets[1].cssRules[1]" is the way
to refer to that object?
You can also add new rules if needed though the API in IE
is different to the API in Mozilla (which follows the W3C
DOM standard:
<http://www.w3.org/TR/DOM-Level-2-Style/> ).


Alright, thanks your prompt help. I think this was the fastest
reply I've ever received on usenet! :)

Fungii

Jul 23 '05 #3


Fungii wrote:

How would I get the rule index
for p:first-letter? Say I have one stylesheet in my page and the
first entry in that stylesheet is the "p:first-letter" entry.
Would that mean "document.styleSheets[1].cssRules[1]" is the way
to refer to that object?


Pretty much all indexing in JavaScript starts with index 0 so
document.styleSheets[0]
is the first style sheet and
documeent.styleSheets[0].cssRules[0]
the first rule in the first style sheet.
You can also loop through the rules and check the selectorText property.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #4
Martin Honnen <ma*******@yahoo.de> wrote in
news:41***********************@newsread4.arcor-online.net:
Pretty much all indexing in JavaScript starts with index 0
so
document.styleSheets[0]
is the first style sheet and
documeent.styleSheets[0].cssRules[0]
the first rule in the first style sheet.
You can also loop through the rules and check the
selectorText property.


Really? I thought Javascript was that weird language that began
their indexes at 1 instead of 0. My bad.

Alright thanks, that should do the trick! :)

Fungii
Jul 23 '05 #5
Fungii wrote:
Martin Honnen <ma*******@yahoo.de> wrote in
news:41***********************@newsread4.arcor-online.net:

Pretty much all indexing in JavaScript starts with index 0
so
document.styleSheets[0]
is the first style sheet and
documeent.styleSheets[0].cssRules[0]
the first rule in the first style sheet.
You can also loop through the rules and check the
selectorText property.


You are better off to loop through all the style sheet and then the
styles. For the sake of development, print out the selectorText and
check in various versions of IE.

From memory, the selectorText for:

.aClass { ... }

will be ".aClass" (whereas the className will be "aClass") for W3C
compliant browsers, but some (older) versions of IE prepend an
asterisk: "*.aClass".

The selector text for "p:first-letter" should be exactly that, but may
be "*p:first-letter" in older IE.

Incidentally, rather than:

var paras = document.all.tags("P");

Use (for cross-browser compatibility):

if (document.getElementsByTagName) {
var paras = document.getElementsByTagName("P");
} else if (document.all) {
var paras = document.all.tags("P");
}

--
Rob
Jul 23 '05 #6
Fungii wrote:
Martin Honnen <ma*******@yahoo.de> wrote in
news:41***********************@newsread4.arcor-online.net:
Pretty much all indexing in JavaScript starts with index 0
so
document.styleSheets[0]
is the first style sheet and
documeent.styleSheets[0].cssRules[0]
the first rule in the first style sheet.
You can also loop through the rules and check the
selectorText property.


Really? I thought Javascript was that weird language that began
their indexes at 1 instead of 0. My bad.

Alright thanks, that should do the trick! :)

Fungii


Maybe this will be of some help. Big disappointment: Opera v.7 tossed
in everything but the kitchen sink - and a document.styleSheets
collection. Just fyi.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<title>untitled</title>
<style type="text/css">

body {
height: 100%;
font-size: 100%;
}
p {
width: 400px;
margin: 160px auto;
font: normal 80% times, sans-serif;
color: darkgreen;
text-align: justify;
padding: 6px;
border: 1px #000 dashed;
background: #fafffa;
cursor: pointer;
}
p:first-letter {
font: bolder 120% times, sans-serif;
color: black;
}

</style>
<script type="text/javascript">

function modCSS(selector /* [attribute/value pairs] */)
{
if ('undefined' != typeof document.styleSheets)
{
var nsheets = document.styleSheets.length,
re = new RegExp('\\b' + selector + '\\b', 'i'),
SS,
rtype,
rule,
rules,
nrules;
for (var nsheet = 0; nsheet < nsheets; ++nsheet)
{
SS = document.styleSheets.item(nsheet);
rtype = ('undefined' != typeof SS.rules) ? 'rules' : 'cssRules';
if ('undefined' != typeof SS[rtype])
{
rules = SS[rtype];
nrules = rules.length;
for (nrule = 0; nrule < nrules; ++nrule)
{
rule = rules.item(nrule);
if (re.test(rule.selectorText))
{
for (var a = 1; a < arguments.length; a += 2)
rule.style[arguments[a]] = arguments[a + 1];
return;
}
}
}
}
}
}

window.onload = function()
{
document.onclick = function()
{
modCSS('p', 'color', 'black', 'fontSize', '90%');
modCSS('p:first-letter','color', 'green', 'fontSize', '160%');
modCSS('body', 'background', 'darkolivegreen');
}
}

</script>
</head>
<body>
<p title="click me">
Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.
</p>
</body>
</html>

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Stephen Poley | last post by:
The issue of the focus pseudo-class came up a few weeks ago, and I finally got around to trying it out (better late than never ...) The recommended order given for the pseudo-classes is link,...
0
by: JW | last post by:
How do I create a WSDL (doc/literal) that represents a soap message which includes ws-addressing elements. The addressing elements sit as header blocks directly inside the header so how is this...
1
by: Steven T. Hatton | last post by:
ISO/IEC 14882:2003: "5.2.4 Pseudo destructor call The use of a pseudo-destructor-name after a dot . or arrow -> operator represents the destructor for the non-class...
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
1
by: Jimbo | last post by:
Hi people, I am playing with the WSE 2.0 SP3 and am trying to get use the ReplyTo WS-Addressing header to send a response to a different machine than where the request came from. However, in my...
0
by: Stefan Lischke | last post by:
Hi, I'm really desperate using code generation(wsdl.exe) from wsdl files for latest WS-Eventing(including WS-Addressing) Specs. I'm writing my diploma about "publish subscribe systems based on...
0
by: Dan | last post by:
I am using the SoapClient and SoapEnvelope classes to call a web service. The SoapEnvelope loads the xml from a file; the xml contains complete description of the envelope, including the addressing...
0
by: gerritmitchell | last post by:
Hi, I have a situation where I need to send a SOAP message from a receiver through multiple intermediaries and then to an ultimate receiver. The intial sender will tell the intermediary where...
1
by: =?Utf-8?B?dWx0cmFuZXQ=?= | last post by:
We have a client that uses .Net that needs to work against our Java (xfire) based WS. My question is: how can a .Net (C#) WS client be configured to not send WS-Addressing headers? The client in...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.