2017-04-20 24 views
0

我正在开发一个API,所以我运行了它,但想对其应用一些样式。如何将样式应用于javascript中的变量

我想改变var title,country,status和summary的样式而没有成功。你可以暗示如何去做?我尝试了一些东西,但没有一个能够工作。

谢谢!

// creating an empty object to hold functionality of our app 
 
var app = {}; 
 

 

 
// create an init method that will hold all the code that must run upon initialization of app 
 
app.init = function(){ 
 
    $('#subject').on('keyup', function(){ 
 
    var subject = $(this).val().toLowerCase(); 
 
    //empty before we ask for result that were there so new results will show up 
 
    $('#showresults').empty(); 
 
    app.getShow(subject); 
 
}); 
 
}; 
 

 
// getShow method will make the Ajax request to the API 
 
app.getShow = function(query){ 
 
    $.ajax({ 
 
     url: 'http://api.tvmaze.com/search/shows?q=:query', 
 
     method: 'GET', 
 
     dataType: 'json', 
 
     data: { 
 
      ps: 20, 
 
      q: query, 
 
      format: 'json' 
 
     }, 
 
     success: function(results){ 
 
      console.log(results); 
 
      app.displayShow(results); 
 
     }, 
 
     error: function(error){ 
 
      console.log(error); 
 
     } 
 
    }); 
 
}; 
 

 

 

 
// displayShow will inject our art pieces into the DOM 
 
app.displayShow = function(ShowArray){ 
 
    //forEach is the equivalent of for loop in jQuery. it is used to loop over our array of show 
 
    ShowArray.forEach(function(showObject){ 
 
     //create a variable that will hold the html and the h that doesn't exist yet. we use artPiece.title because it's the place holder for the object 
 
     var title = showObject.show.name ; 
 
     var country = showObject.show.network.country.name 
 
     var status= showObject.show.status ; 
 
     var summary = showObject.show.summary ; 
 
     //Adding all of our elements into this div 
 

 
     var showHtml = $('<div>').addClass('series').append(title + '<br>' + country + '<br>' + status + summary); 
 
     $('#showresults').append(showHtml); 
 
    }); 
 
}; 
 

 

 
$(function(){ // shortform of document.ready, which waits for all of the HTML document to be loaded before running JS 
 
    app.init(); 
 
});
article,aside,details,figcaption,figure,footer,header,hgroup,nav,section,summary{display:block;}audio,canvas,video{display:inline-block;}audio:not([controls]){display:none;height:0;}[hidden]{display:none;}html{font-family:sans-serif;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;}a:focus{outline:thin dotted;}a:active,a:hover{outline:0;}h1{font-size:2em;}abbr[title]{border-bottom:1px dotted;}b,strong{font-weight:700;}dfn{font-style:italic;}mark{background:#ff0;color:#000;}code,kbd,pre,samp{font-family:monospace, serif;font-size:1em;}pre{white-space:pre-wrap;word-wrap:break-word;}q{quotes:\201C \201D \2018 \2019;}small{font-size:80%;}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline;}sup{top:-.5em;}sub{bottom:-.25em;}img{border:0;}svg:not(:root){overflow:hidden;}fieldset{border:1px solid silver;margin:0 2px;padding:.35em .625em .75em;}button,input,select,textarea{font-family:inherit;font-size:100%;margin:0;}button,input{line-height:normal;}button,html input[type=button],/* 1 */ 
 
input[type=reset],input[type=submit]{-webkit-appearance:button;cursor:pointer;}button[disabled],input[disabled]{cursor:default;}input[type=checkbox],input[type=radio]{box-sizing:border-box;padding:0;}input[type=search]{-webkit-appearance:textfield;-moz-box-sizing:content-box;-webkit-box-sizing:content-box;box-sizing:content-box;}input[type=search]::-webkit-search-cancel-button,input[type=search]::-webkit-search-decoration{-webkit-appearance:none;}textarea{overflow:auto;vertical-align:top;}table{border-collapse:collapse;border-spacing:0;}body,figure{margin:0;}legend,button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0;} 
 

 
.clearfix:after {visibility: hidden; display: block; font-size: 0; content: " "; clear: both; height: 0; } 
 

 
* { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; } 
 

 
body { 
 
    font-family: "Roboto", "Arial", sans-serif; 
 
    font-size: 20px; 
 
} 
 

 
main { 
 
    background: #0B1C56; 
 
    color: #fff; 
 
} 
 

 
.container { 
 
    max-width: 1200px; 
 
    margin: 0 auto; 
 
    padding: 50px; 
 
    overflow: hidden; /*clearfix*/ 
 
} 
 

 
h1 { 
 
    margin-top: 0; 
 
}
<!doctype html> 
 
<html lang="en"> 
 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title>TV SHOW APP</title> 
 
    <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet"> 
 
    <link rel="stylesheet" href="style.css"> 
 
</head> 
 

 
<body> 
 
    <header> 
 
    <div class="container"> 
 
     <h1 id="page-title">TV SHOW APP</h1> 
 

 

 
<!---ADDED----> 
 

 
     <form> 
 
     <label for="subject">Choose your show</label> 
 
     <input name="subject" id="subject"> 
 
     </input> 
 
     
 
     </form> 
 

 
<!---END OF ADDED----> 
 
</div> 
 
</header> 
 

 
    <main> 
 
    <div class="container" id="showresults"></div> 
 
    </main> 
 

 
    <script src="http://code.jquery.com/jquery-3.1.1.min.js" 
 
      integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" 
 
      crossorigin="anonymous"> 
 
    </script> 
 
    <script src="app.js"></script> 
 
</body> 
 
</html>

+0

你能更好地解释你想做什么? –

回答

1

你可以尝试这样的事情

var showHtml = $('<div>').addClass('series') 
.append('<span class="titleClass">' +title +'</span>' 
+ '<br> <span class="countryeClass">' + country +'</span>' 
+ '<br> <span class="statusClass">' + status + summary+'</span>'); 
0

你想包装在HTML元素的变量输出,给要素类或某种标识符,然后使用CSS来设计它们。

在这里,我将输出文本包装在span s中,但您可以使用任何适合的方法。

var showHtml = $('<div>').addClass('series').append('<span class="title">' + title + '</span>' + '<br>' + '<span class="country">' + country + '</span>' + '<br>' + '<span class="status">' + status + '</span>' + '<span class="summary">' + summary + '</span>'); 

下面是一些例子CSS样式他们

.title { 
    color: red; 
} 
.country { 
    color: white; 
} 
.status { 
    color: blue; 
} 
.summary { 
    color: lemonchiffon; 
} 
相关问题