• Das Erstellen neuer Accounts wurde ausgesetzt. Bei berechtigtem Interesse bitte Kontaktaufnahme über die üblichen Wege. Beste Grüße der Admin

[FRAGE] JS; bild mit if abfrage wählen

rio-doro

New member
Guten abend allerseits :)
Ich habe eine HTML welche ein bild an einer bestimmten stelle anzeigt, nun möchte ich in einem Javascript definieren, das wenn ich in einer datei namens Config.js den wert änder, die html das jeweils andere bild anzeigt.
Kann mir da jemand helfen?
Die Config.js:
Code:
var png = "1";
Die html:
Code:
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<link rel="stylesheet" type="text/css" href="main.css">
<style>
#png {margin-top: -8px; }
#png {margin-left: -8px; }
</style>
</head>
<body>
      <div id="png"><img src="1.png" alt="bild" width="102%"/></div>
	  </body>
          </div>
      </div>
    </div>
</html>
 
Eigentlich geht das besser mit PHP:

config.php: <?php $png = 1; ?>

Einziehen mit <?php include('config.php'); ?>

Und im img-Tag:
PHP:
<img src="<?php echo $png; ?>.png" alt="bild" width="102%"/></div>

Es würde aber auch in Javascript mit der jQuery-Funktion get() gehen.
 
Zuletzt bearbeitet:
vielen dank :)
Leider bin ich durch die bestimmung an ein Programm und dadurch an js gebunden.
Hat sich aber auch erledigt, ein freund konnte mir schon helfen :)

- - - Aktualisiert - - -

Falls es jemandwn interessiert, hier meine fertigen codes :D

Die script.js:
Code:
function bild() {
		if(background == "1"){	document.body.style.backgroundImage = "url(1.png)";
		}
if(background == "2"){	document.body.style.backgroundImage = "url(2.png)"
		}
		if(background == "3"){	document.body.style.backgroundImage = "url(3.png)"
		}
				if(background == "4"){	document.body.style.backgroundImage = "url(4.png)";
		}
if(background == "5"){	document.body.style.backgroundImage = "url(5.png)"
		}
		if(background == "6"){	document.body.style.backgroundImage = "url(6.png)"
		}
				if(background == "7"){	document.body.style.backgroundImage = "url(7.png)";
		}
if(background == "8"){	document.body.style.backgroundImage = "url(8.png)"
		}
		if(background == "9"){	document.body.style.backgroundImage = "url(9.png)"
		}
				if(background == "10"){	document.body.style.backgroundImage = "url(10.png)";
		}
if(background == "11"){	document.body.style.backgroundImage = "url(11.png)"
		}
		if(background == "12"){	document.body.style.backgroundImage = "url(12.png)"
		}
}

Die html:
Code:
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<link rel="stylesheet" type="text/css" href="main.css">
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript" src="Config.js"></script>
<style>
#bild {margin-top: -8px; }
#bild {margin-left: -8px; }
#bild {width: 102%; }
</style>
</head>
<body onload="bild();">
    
	  </body>
          
</html>

und die config.js:
Code:
var background = "1"; //Set the digit of your favourite theme, you can see the themes in the screenshots of this package. PLEASE RESPRING AFTER SAVE!
 
Manchmal sieht man den Wald vor lauter Bäumen nicht und es war mir inzwischen auch eingefallen: Da deine config.js Javascript-Syntax hat, braucht es natürlich kein Ajax und kein PHP, sondern man kann sie über ein simples Skript-Tag einbinden.
Das in der Funktion Bild() geht aber viel einfacher:
function bild() {document.body.style.backgroundImage = "url(" + background + ".png)";}
 
Zurück
Oben