jQuery和JS函数不能正确加载到页面中

jQuery and JS function not loading properly into page

本文关键字:加载 JS 函数 不能 jQuery      更新时间:2023-09-26

我正在寻找使用jQuery和JavaScript动态添加内容到我的页面,但有加载内容的问题。我已经创建了4个单独的对象,当我用每个参数(book1、book2、album1、album2)调用它时,我将使用一个函数将每个对象的内容添加到页面中。我希望能够有页面加载的对象和各自的内容页面如下:book1, book2, album1, album2。

目前,然而,当我加载页面时,我看到book2(少年派的奇幻漂流)的"名称","类别"answers"价格"属性值,以及book1在每个4div上的"selling_points"值。

*当我检查控制台时,我目前得到一个"未捕获的类型错误:无法读取未定义的属性'forEach'",用于"product.selling_points.forEach(function(point))"部分代码。

另外,我不确定如何编写代码来添加每个对象的各自的picture_url;我目前只是直接添加img url,我想为book1使用到每个div。

body {
  background-color: green;
}
#header {
  background-color: red;
  font-size:300%;
  text-align: center;
  color: purple;
  font-weight: bold;
}
#navbar {
  background-color:blue;
  text-align: center;
}
#content {
  background-color: red;
  text-align: center;
  font-size:150%;
  font-style: oblique;
  color:darkblue;
}
#book1 {
  background-color: red;
  font: black;
}
#book2 {
  background-color: red;
  font: black;
}
#album1 {
  background-color: red;
  font: black;
}
#album2 {
  background-color: red;
  font: black;
}
.image {
  height:600px;
  width:420px;
  display: block;
  margin-left: auto;
  margin-right: auto;
}
p {
  font-size:120%;
  text-align: center;
}
.name {
  font-size: 150%;
}
  background-color: red;
  font: black;
}
#album1 {
  background-color: red;
  font: black;
}
#album2 {
  background-color: red;
  font: black;
}
.image {
  height:600px;
  width:420px;
}
<html>
<head>
  <link rel="stylesheet" type="text/css" href="styles.css">
  <title>Blamazon</title>
</head>
<body>
<div id="header">BLAMAZON</div><br>
<div id="content">Products</div><br>
  <div id="book1">
    <p class="name"></p>
    <p class="category"></p>
    <p class="price"></p>
    <img class="image">
    <p class="description"></p>
  </div>
  <div id="book2">
    <p class="name"></p>
    <p class="category"></p>
    <p class="price"></p>
    <img class="image">
    <p class="description"></p>
  </div>
  <div id="album1">
    <p class="name"></p>
    <p class="category"></p>
    <p class="price"></p>
    <img class="image">
    <p class="description"></p>
  </div>
  <div id="album2">
    <p class="name"></p>
    <p class="category"></p>
    <p class="price"></p>
    <img class="image">
    <p class="description"></p>
  </div>
<div id="footer"></div>
<script src="https://code.jquery.com/jquery-2.1.1.js"></script>
<script>
var book1, book2, album1, album2
book1 = {
    product_id: 'book1',
    "name": "Thinking Fast and Slow",
    "category": "Non-Fiction",
    "picture_url": "http://ecx.images-amazon.com/images/I/51oXKWrcYYL.jpg",
    "price": "$7.07",
    "selling_points": ["This will help you make better decisions!", "Understand how humans make the wrong decisions so often"]
}
book2 = {
    product_id: 'book2',
    "name": "The Life of Pi",
    "category": "Fiction",
    "picture-url": "http://bestfreebooks.org/wp-content/uploads/2015/07/Life-of-Pi-Yann-Martel.jpg",
    "price": "$9.07",
    "selling_points": ["This will make you never want to get on a boat with a tiger...", "And understand the meaning of life!"]
}
album1 = {
    product_id: 'album1',
    "name": "Back in Black, AC DC",
    "category": "Hard Rock",
    "picture_url": "https://upload.wikimedia.org/wikipedia/commons/b/be/Acdc_backinblack_cover.jpg",
    "price": "$12.07",
    "selling_points": ["Oldie but a goodie", "Will help you rock out"]
}
album2 = {
    product_id: 'album2',
    "name": "Good kid maad city",
    "category": "Hip-Hop",
    "picture_url": "http://ecx.images-amazon.com/images/I/51Zzc7PUDML._SY300_.jpg",
    "price": "$12.07",
    "selling_points": ["A sprawling masterpiece of technical rapping and structured storytelling", "Defies and expands the conventions of the genre"]
}
var add_product = function(product) {
    var $prod = $('#' + product.product_id)
    $prod.find('.name').text(product.name)
    $prod.find('.category').text(product.category)
    $prod.find('.image').attr('src','http://ecx.images-amazon.com/images/I/51oXKWrcYYL.jpg')
    $prod.find('.price').text(product.price)
    product.selling_points.forEach(function(point) {
        $prod.find('.description').append("<div><br>" + point + "</div><br>")
    })
}
add_product(book1)
add_product(book2)
add_product(album1)
add_product(album2)
/*
  var add_product = function(product) {
    $('.name').text(product.name)
    $('.category').text(product.category)
    $('.image').attr('src','http://ecx.images-amazon.com/images/I/51oXKWrcYYL.jpg')
    $('.price').text(product.price)
    product.selling_points.forEach(function(point) {
      $('.description').append("<div><br>" + point + "</div><br>")
    })
  }
  add_product(book1)
  add_product(book2)
  add_product(album1)
  add_product(album2)
*/
</script>
</body>
</html>

实际上你只是在album1中出现了一个错别字——你用了selling-points而不是selling_points。我认为你的脚本加载是好的。在不正确的位置有描述等的原因是$(.classname)找到页面中具有类名的所有元素。尝试通过添加product_id来添加限定符,并找到相对于限定符的类名—如下所示:

var book1, book2, album1, album2
book1 = {
    product_id: 'book1',
    "name": "Thinking Fast and Slow",
    "category": "Non-Fiction",
    "picture_url": "http://ecx.images-amazon.com/images/I/51oXKWrcYYL.jpg",
    "price": 7.07,
    "selling_points": ["This will help you make better decisions!", "Understand how humans make the wrong decisions so often"]
}
book2 = {
    product_id: 'book2',
    "name": "The Life of Pi",
    "category": "Fiction",
    "picture-url": "http://bestfreebooks.org/wp-content/uploads/2015/07/Life-of-Pi-Yann-Martel.jpg",
    "price": 9.07,
    "selling_points": ["This will make you never want to get on a boat with a tiger...", "And understand the meaning of life!"]
}
album1 = {
    product_id: 'album1',
    "name": "Back in Black, AC DC",
    "category": "Hard Rock",
    "picture_url": "https://upload.wikimedia.org/wikipedia/commons/b/be/Acdc_backinblack_cover.jpg",
    "price": 12.07,
    "selling_points": ["Oldie but a goodie", "Will help you rock out"]
}
album2 = {
    product_id: 'album2',
    "name": "Good kid maad city",
    "category": "Hip-Hop",
    "picture_url": "http://ecx.images-amazon.com/images/I/51Zzc7PUDML._SY300_.jpg",
    "price": 12.07,
    "selling_points": ["A sprawling masterpiece of technical rapping and structured storytelling", "Defies and expands the conventions of the genre"]
}
var add_product = function(product) {
    var $prod = $('#' + product.product_id)
    $prod.find('.name').text(product.name)
    $prod.find('.category').text(product.category)
    $prod.find('.image').attr('src','http://ecx.images-amazon.com/images/I/51oXKWrcYYL.jpg')
    $prod.find('.price').text(product.price)
    product.selling_points.forEach(function(point) {
        $prod.find('.description').append("<div><br>" + point + "</div><br>")
    })
}
add_product(book1)
add_product(book2)
add_product(album1)
add_product(album2)

您试图在.js文件中加载jQuery文件。你也有<script></script>标签在你的文件。这些不是必需的,可能会破坏正在读取的文件。

应该在HTML中加载JS。

如果你真的需要将jQuery加载到JS文件中,我建议使用以下代码:

var script = document.createElement('script');
script.src = 'https://code.jquery.com/jquery-2.1.1.js';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);

但这实际上只是将代码附加到HTML中。

  var book1, book2, album1, album2
  book1 = {
    "name": "Thinking Fast and Slow",
    "category": "Non-Fiction",
    "picture_url": "http://ecx.images-amazon.com/images/I/51oXKWrcYYL.jpg",
    "price": 7.07,
    "selling_points": ["This will help you make better decisions!", "Understand how humans make the wrong decisions so often"]
  }
  book2 = {
    "name": "The Life of Pi",
    "category": "Fiction",
    "picture-url": "http://bestfreebooks.org/wp-content/uploads/2015/07/Life-of-Pi-Yann-Martel.jpg",
    "price": 9.07,
    "selling_points": ["This will make you never want to get on a boat with a tiger...", "And understand the meaning of life!"]
  }
  album1 = {
    "name": "Back in Black, AC DC",
    "category": "Hard Rock",
    "picture_url": "https://upload.wikimedia.org/wikipedia/commons/b/be/Acdc_backinblack_cover.jpg",
    "price": 12.07,
    "selling-points": ["Oldie but a goodie", "Will help you rock out"]
  }
  album2 = {
    "name": "Good kid maad city",
    "category": "Hip-Hop",
    "picture_url": "http://ecx.images-amazon.com/images/I/51Zzc7PUDML._SY300_.jpg",
    "price": 12.07,
    "selling_points": ["A sprawling masterpiece of technical rapping and structured storytelling", "Defies and expands the conventions of the genre"]
  }
  var add_product = function(product) {
    $('.name').text(product.name)
    $('.category').text(product.category)
    $('.image').attr('src','http://ecx.images-amazon.com/images/I/51oXKWrcYYL.jpg')
    $('.price').text(product.price)
    product.selling_points.forEach(function(point) {
      $('.description').append("<div><br>" + point + "</div><br>")
    })
  }
  add_product(book1)
  add_product(book2)
  add_product(album1)
  add_product(album2)
body {
  background-color: green;
}
#header {
  background-color: purple;
  font-size:200%;
  text-align: center;
}
#navbar {
  background-color:blue;
  text-align: center;
}
#content {
  background-color: red;
  text-align: center;
}
#book1 {
  background-color: red;
  font: black;
}
#book2 {
  background-color: red;
  font: black;
}
#album1 {
  background-color: red;
  font: black;
}
#album2 {
  background-color: red;
  font: black;
}
.image {
  height:600px;
  width:420px;
}
<html>
<head>
  <link rel="stylesheet" type="text/css" href="styles.css">
  <title>Blamazon</title>
  <script src="https://code.jquery.com/jquery-2.1.1.js"></script>
</head>
<body>
<div id="header">BLAMAZON</div><br>
<div id="content">Products</div><br>
  <div id="book1">
    <p class=name></p>
    <p class=category></p>
    <p class=price></p>
    <img class="image"></p>
    <p class="description"></p>
  </div>
  <div id="book2">
    <p class=name></p>
    <p class=category></p>
    <p class=price></p>
    <img class="image"></p>
    <p class="description"></p>
  </div>
  <div id="album1">
    <p class=name></p>
    <p class=category></p>
    <p class=price></p>
    <img class="image"></p>
    <p class="description"></p>
  </div>
  <div id="album2">
    <p class=name></p>
    <p class=category></p>
    <p class=price></p>
    <img class="image"></p>
    <p class="description"></p>
  </div>
<div id="footer"><div>

为将来参考,打开你的浏览器控制台日志,它会显示你Javascript中的所有错误。

var book1, book2, album1, album2;
book1 = {
    "name": "Thinking Fast and Slow",
        "category": "Non-Fiction",
        "picture_url": "http://ecx.images-amazon.com/images/I/51oXKWrcYYL.jpg",
        "price": 7.07,
        "selling_points": ["This will help you make better decisions!", "Understand how humans make the wrong decisions so often"],
    add: function () {
        $('#book1 .name').text(this.name);
        $('#book1 .category').text(this.category);
        $('#book1 .price').text(this.price);
        $('#book1 .image').attr('src', this.picture_url);
        $('#book1 .name').text(this.name);
        this.selling_points.forEach(function (point) {
            $('#book1 .description').append("<div><br>" + point + "</div><br>");
        });
    }
}
book2 = {
    "name": "The Life of Pi",
        "category": "Fiction",
        "picture_url": "http://bestfreebooks.org/wp-content/uploads/2015/07/Life-of-Pi-Yann-Martel.jpg",
        "price": 9.07,
        "selling_points": ["This will make you never want to get on a boat with a tiger...", "And understand the meaning of life!"],
    add: function () {
        $('#book2 .name').text(this.name);
        $('#book2 .category').text(this.category);
        $('#book2 .price').text(this.price);
        $('#book2 .image').attr('src', this.picture_url);
        $('#book2 .name').text(this.name);
        this.selling_points.forEach(function (point) {
            $('#book2 .description').append("<div><br>" + point + "</div><br>");
        });
    }
}
album1 = {
    "name": "Back in Black, AC DC",
        "category": "Hard Rock",
        "picture_url": "https://upload.wikimedia.org/wikipedia/commons/b/be/Acdc_backinblack_cover.jpg",
        "price": 12.07,
        "selling_points": ["Oldie but a goodie", "Will help you rock out"],
    add: function () {
        $('#album1 .name').text(this.name);
        $('#album1 .category').text(this.category);
        $('#album1 .price').text(this.price);
        $('#album1 .image').attr('src', this.picture_url);
        $('#album1 .name').text(this.name);
        this.selling_points.forEach(function (point) {
            $('#album1 .description').append("<div><br>" + point + "</div><br>");
        });
    }
}
album2 = {
    "name": "Good kid maad city",
        "category": "Hip-Hop",
        "picture_url": "http://ecx.images-amazon.com/images/I/51Zzc7PUDML._SY300_.jpg",
        "price": 12.07,
        "selling_points": ["A sprawling masterpiece of technical rapping and structured storytelling", "Defies and expands the conventions of the genre"],
    add: function () {
        $('#album2 .name').text(this.name);
        $('#album2 .category').text(this.category);
        $('#album2 .price').text(this.price);
        $('#album2 .image').attr('src', this.picture_url);
        $('#album2 .name').text(this.name);
        this.selling_points.forEach(function (point) {
            $('#album2 .description').append("<div><br>" + point + "</div><br>");
        });
    }
}
book1.add();
book2.add();
album1.add();
album2.add();