Jump to content
php.lv forumi
  • 0

Kā nomainīt fonu linkam?


Sephy

Question

Sveiki te mans kods!

 

<script>
function changeStyle()
{
document.getElementById("leftstyle").style.backgroundImage="url(bildes/topm_act.png)";
}
function changeStyleBack()
{
document.getElementById("leftstyle").style.backgroundImage="";
}
</script>

<div id="left_sidebar">
 <div id="menuleft">
 <div id="AccordionContainer" class="AccordionContainer">
  <div onclick="runAccordion(1);">
<div class="AccordionTitle" onselectstart="return false;">
<a id="leftstyle" href="#">Tituls</a>
</div>
  </div>
  <div id="Accordion1Content" class="AccordionContent" onclick="changeImage()">
<a id="leftstyle" href="#" onmouseover="changeStyle()" onmouseout="changeStyleBack()">derp</a>
<a id="leftstyle" href="#" onmouseover="changeStyle()" onmouseout="changeStyleBack()">hurr</a>
  </div>
  <a id="leftstyle" href="#">Sekcija2</a>
 </div>
 </div>
</div>

Problema ir tur ka maina stilu nevis tam linkam kura ir changestyle, bet gan Titul linkā ;/

Izskatās ka maina tikai pirmajā leftstyle un viss, kā var uzlikt lai esošajā leftstyle maina linku!!

Edited by Sephy
Link to comment
Share on other sites

13 answers to this question

Recommended Posts

  • 0

No JS gan diezgan maz "rubiju", bet šķiet ka katra elementa ID ir jābūt unikālam. t.i nevar būt vairāki elementi ar vienādu ID

 

Garāks gan sanāktu pieraksts bet, varētu arī šādi:

<a href="#" onmouseover="this.style.backgroundImage='url(bildes/topm_act.png)'" onmouseout="this.style.backgroundImage=''">derp</a>
<a href="#" onmouseover="this.style.backgroundImage='url(bildes/topm_act.png)'" onmouseout="this.style.backgroundImage=''">hurr</a>

 

Kautkā laikam varēja arī ar document.getelementbyNames('leftstyle').style... vai document.all.leftstyle.style...

tikai lai tiem kam vajag mainīt backu "leftstyle" ir nevis ID bet gan name.

 

Tiesa gan man pašam nav diezko veicies ar šiem variantiem

Edited by nikidijs
Link to comment
Share on other sites

  • 0

1. kā jau tika minēts ID jābūt unikālam !!!

2. padod funkcijai to ID

 

<script>
function changeStyle(mainigais)
{
document.getElementById(mainigais).style.backgroundImage="url(bildes/topm_act.png)";
}
function changeStyleBack(mainigais)
{
document.getElementById(mainigais).style.backgroundImage="";
}
</script>
<a id="leftstyle" href="#" onmouseover="changeStyle(this.id)" onmouseout="changeStyleBack(this.id)">derp</a>
<a id="leftstyle" href="#" onmouseover="changeStyle(this.id)" onmouseout="changeStyleBack(this.id)">hurr</a>

 

tas tas uz ātru roku ...

Link to comment
Share on other sites

  • 0

kāpēc divas funkcijas, un priekš kam id, ja reāli netiek nekam izmantots?

<script type="text/javascript">
function changeStyle(el, off) {
 el.style.backgroundImage = !off ? "url(bildes/topm_act.png)" : "";
}
</script>

<a href="#" onmouseover="changeStyle(this)" onmouseout="changeStyle(this, 1)">derp</a>
<a href="#" onmouseover="changeStyle(this)" onmouseout="changeStyle(this, 1)">hurr</a>

Edited by indoom
Link to comment
Share on other sites

  • 0

1. jQuery

 

2. konkrēti šo uzdevumu var atrisināt bez js:

http://jsfiddle.net/An6p3/2/

 

html:

<a class="mylink" href="#">HELLO</a><br />
<a class="mylink" href="#">HELLO</a><br />
<a class="mylink" href="#">HELLO</a><br />

css:

a.mylink:hover{   
 background:url(http://www.ferdychristant.com/blog//resources/Fun/$FILE/shadedbutton11.gif);
}

Edited by codez
Link to comment
Share on other sites

  • 0

Papildjautājums! Vai var pielikt, lai nomaina background ne tikai linkam, bet arī kādam citam elementam? Piemeram leftsidebar backgroundu nomainit!

 

function changecolor(el,off){
 el.style.color = !off ? '#90003a': '';
 left_sidebar.style.backgroundImage = !off ? "url(bildes/bk/bildes.png)" : "";
}

 

Šitā te nestrādā ;/

Edited by Sephy
Link to comment
Share on other sites

  • 0

http://jsfiddle.net/rw5es/

 

js:

$('.mylink').hover(function(){
$(this).stop(true,true).animate({backgroundColor:'#f00'},500);
$('.leftsidebar').stop(true,true).animate({backgroundColor:$(this).attr('color')},1500);
},function(){
$(this).stop(true,true).animate({backgroundColor:'#fff'},500);
$('.leftsidebar').stop(true,true).animate({backgroundColor:'#fff'},1500);
});

 

html:

 

<div class="leftsidebar">left sidebar</div>
<div class="content">
<a class="mylink" color="#f00" href="#">HELLO</a>
<a class="mylink" color="#0f0"  href="#">HELLO</a>
<a class="mylink" color="#00f"  href="#">HELLO</a>
</div>
<div class="clear"></div>

Link to comment
Share on other sites

  • 0

Neesmu iebraucis problēmā, bet:

 

 

$('body').click(function () {

   if ($(this).css(...) == 'red') {

       $(this).css(..., ...);

   } else {

       $(this).css(..., ...);

   }

   $('#foo').css(...); // Naggagaga.

}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...