son Posted June 10, 2010 Share Posted June 10, 2010 I try to change font colour and background colour for current link as: window.onload = function() { var navbars = new Array("topNav", "sideNav"); // navbars for (j = 0; j < navbars.length; j++) { anchors = document.getElementById(navbars[j]).getElementsByTagName("a"); var url = window.location.href.split("?")[0]; for (i = 0; i < anchors.length; i++) { if (anchors[i].href == url) { anchors[i].style.color = "red"; anchors[i].style.background-color = "white"; } } }} Text works, but not bg color... Why does this not work?Son Link to comment Share on other sites More sharing options...
dsonesuk Posted June 10, 2010 Share Posted June 10, 2010 try backgroundColor = Link to comment Share on other sites More sharing options...
son Posted June 11, 2010 Author Share Posted June 11, 2010 try backgroundColor =That solved it. Cheers!Son Link to comment Share on other sites More sharing options...
son Posted June 11, 2010 Author Share Posted June 11, 2010 That solved it. Cheers!SonDoes anchors.style.backgroundImage = 'url(navBG_green.png)'; anchors.style.backgroundRepeat = 'repeat';not work? Tried it, but without luck...Son Link to comment Share on other sites More sharing options...
dsonesuk Posted June 11, 2010 Share Posted June 11, 2010 should work, try thisfunction dothis(){anchors = document.getElementsByTagName("a");for(i=0;i<anchors.length;i++){anchors.style.backgroundImage='url(right.png)';}}window.onload=dothis; Link to comment Share on other sites More sharing options...
Rufus Posted June 11, 2010 Share Posted June 11, 2010 Just a little clarification on dsonesuk's fix, JavaScript messed up when it was created and they didn't acknowledge the guys working on CSS. So, we have the same style properties but they have different names.I always keep this as a reference. http://codepunk.hardwar.org.uk/css2js.htm Link to comment Share on other sites More sharing options...
son Posted June 11, 2010 Author Share Posted June 11, 2010 should work, try thisfunction dothis(){anchors = document.getElementsByTagName("a");for(i=0;i<anchors.length;i++){anchors.style.backgroundImage='url(right.png)';}}window.onload=dothis; Still does not work... Could it be a cross-browser issue? I work with Firefox...SonSon Link to comment Share on other sites More sharing options...
dsonesuk Posted June 11, 2010 Share Posted June 11, 2010 i tested this in firefox! heres the whole script<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Untitled Document</title><script type="text/javascript">/*<![CDATA[*//*---->*/function dothis(){anchors = document.getElementsByTagName("a");for(i=0;i<anchors.length;i++){anchors.style.backgroundImage='url(right.png)';}}window.onload=dothis;/*--*//*]]>*/</script> <style type="text/css"></style></head><body><p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex.</p> <a href="#">aaaaaaaaaaaaa</a><a href="#">bbbbbbbbbbbbb</a><a href="#">ccccccccccccc</a><a href="#">dddddddddddddd</a><a href="#">eeeeeeeeeeeeeeeee</a><a href="#">fffffffffffffffffff</a></body></html> Link to comment Share on other sites More sharing options...
son Posted June 11, 2010 Author Share Posted June 11, 2010 You are right, this one is working... Strange, maybe I messed something up when I inserted code into external javascript file...Cheers,Son Link to comment Share on other sites More sharing options...
jeffman Posted June 11, 2010 Share Posted June 11, 2010 . . . JavaScript messed up when it was created and they didn't acknowledge the guys working on CSS. So, we have the same style properties but they have different names.More likely the other way around. If the CSS team even imagined JavaScript would ever have a CSS interface, they should have realized that a property like background-image simply cannot exist. The hyphen will be read as a subtraction operator, and there is no way around that. (Even overloading the operator wouldn't work, I think, since a hyphen really isn't an operator.) If any additional thinking needed to happen, it should have been the CSS team to do it.Most likely, the groups weren't even thinking about each other. In 1996, plain-vanilla JavaScript couldn't even get a reference to most page elements, because the id attribute didn't exist yet, so this kind of dynamic manipulation would not have been thought possible. Those were the days . . . Link to comment Share on other sites More sharing options...
dsonesuk Posted June 12, 2010 Share Posted June 12, 2010 I agree you can’t blame JavaScript developers, JavaScript started to became available in 1995-96, while a type of css started to appear in 1993, which looks nothing like css we use at present, and is quiet frightening to look at, shudder... The styling was not fully defined as a standard, and many methods on how to implement styling where introduced until css1 W3C Recommendation came out in 1996. But four months before that a JavaScript-Based Styling was unleashed, which they could not have possibly have used current styling because it would have been totally impractical and so they used their own reference for styling. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.