1
Någon som kan lite om javascript?
Postat av flang den 19 September 2014, 00:24
7 kommentarer · 721 träffar
Tjenare! jag skulle behöva lite hjälp här, har en gammal skoluppgift som ligger över mig som jag ej får rätt på... Jag har en mapp med 4 stycken json filer som jag vill ska kopplas med min kod, men det funkar ej, någon som kan se problemet på rak hand?
ty!
Här kommer min htmlkod:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id='meny'>
<button>No article</button>
<button>article1</button>
<button>article2</button>
<button>article3</button>
<button>article4</button>
<button>article5</button>
</div>
<div id='div2'>
<p id='selectedArticle'>No article selected</p>
<div>Författare:<a href="" id='author'></a></div>
<div>Datum:<span id='date'></span></div>
<p id='text'>select article</p>
</div>
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script src="script.js"></script>
<meta charset="UTF-8">
</body>
</html>
OCH här kommer min JS:
$(document).ready(function(){
var buttons = $("button");
var setFirstButtonBold = function(){
buttons.first().css("font-weight","bold");
};
setFirstButtonBold();
$('#meny button').on('click',function () {
buttons.css("font-weight","normal");
$(this).css("font-weight","bold");
var buttonName = $(this).text();
if($(this).text() == "No article"){
$('#selectedArticle').html("No article selected");
$('#author').attr("");
$('#author').html("");
$('#date').text("");
$('#text').html("select article");
}else{
$('#text').text("Laddar....");
}
$.get('articles/'+buttonName,function(data){
$('#selectedArticle').html(data.title);
$('#author').attr("href","mailto:"+data.author);
$('#author').html(data.author);
$('#date').text(data.date);
$('#text').html(data.text);
},'json');
});
});
ty!
Här kommer min htmlkod:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id='meny'>
<button>No article</button>
<button>article1</button>
<button>article2</button>
<button>article3</button>
<button>article4</button>
<button>article5</button>
</div>
<div id='div2'>
<p id='selectedArticle'>No article selected</p>
<div>Författare:<a href="" id='author'></a></div>
<div>Datum:<span id='date'></span></div>
<p id='text'>select article</p>
</div>
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script src="script.js"></script>
<meta charset="UTF-8">
</body>
</html>
OCH här kommer min JS:
$(document).ready(function(){
var buttons = $("button");
var setFirstButtonBold = function(){
buttons.first().css("font-weight","bold");
};
setFirstButtonBold();
$('#meny button').on('click',function () {
buttons.css("font-weight","normal");
$(this).css("font-weight","bold");
var buttonName = $(this).text();
if($(this).text() == "No article"){
$('#selectedArticle').html("No article selected");
$('#author').attr("");
$('#author').html("");
$('#date').text("");
$('#text').html("select article");
}else{
$('#text').text("Laddar....");
}
$.get('articles/'+buttonName,function(data){
$('#selectedArticle').html(data.title);
$('#author').attr("href","mailto:"+data.author);
$('#author').html(data.author);
$('#date').text(data.date);
$('#text').html(data.text);
},'json');
});
});