如何在javascript

时间:2017-04-20 18:35:18

标签: javascript css ajax object

我正在开发一个API,所以我让它运行但是想要应用一些样式。

我正在尝试更改var标题,国家/地区,状态和摘要的样式而没有成功。你能否暗示如何做到这一点?我尝试过一些东西,但没有一个在起作用。

谢谢!

// 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>

2 个答案:

答案 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>' );

答案 1 :(得分:0)

您希望将这些变量的输出包装在HTML元素中,为元素提供类或某种标识符,然后使用CSS对它们进行样式化。

这里我将输出文本包装在span中,但你可以在这里使用任何合适的内容。

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;
}