JQuery appendTo是复制元素

时间:2017-09-08 08:23:54

标签: jquery append

Hello由于某种原因,当我附加到c_group时,它会复制每个元素两次。我没有与appendTo的任何其他问题相同的问题。 Result of JQuery 我不知道为什么会这样。有谁知道为什么?为什么它只发生在c_group而没有别的。

我已经厌倦了很多东西来解决这个问题,我相信它复制了第二个我创建元素甚至在appendTo之前,但我无法确定

谢谢, 丹

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: daniel
  Date: 07/09/17
  Time: 09:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<c:set var="scheme" value="${pageContext.request.scheme}"/>
<c:set var="port" value="${pageContext.request.serverPort}"/>
<c:set var="baseUrl" value="${scheme}://${pageContext.request.serverName}:${port}${pageContext.request.contextPath}/" />

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">

    <title>Starter Template for Bootstrap</title>

    <!-- Bootstrap core CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">

    <!-- Custom styles for this template -->
    <link href="starter-template.css" rel="stylesheet">

    <style>
        body {
            padding-top: 5rem;
        }
        .starter-template {
            padding: 3rem 1.5rem;
            text-align: center;
        }

    </style>
</head>

<body>

<nav class="navbar navbar-toggleable-md navbar-inverse bg-inverse fixed-top">
    <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <a class="navbar-brand" href="#">Navbar</a>

    <div class="collapse navbar-collapse" id="navbarsExampleDefault">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item active">
                <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Link</a>
            </li>
            <li class="nav-item">
                <a class="nav-link disabled" href="#">Disabled</a>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="http://example.com" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
                <div class="dropdown-menu" aria-labelledby="dropdown01">
                    <a class="dropdown-item" href="#">Action</a>
                    <a class="dropdown-item" href="#">Another action</a>
                    <a class="dropdown-item" href="#">Something else here</a>
                </div>
            </li>
        </ul>
        <form class="form-inline my-2 my-lg-0">
            <input class="form-control mr-sm-2" type="text" placeholder="Search">
            <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
        </form>
    </div>
</nav>

<div class="container">

    <div class="starter-template">
        <div id="content"></div>
        <h1>Bootstrap starter template</h1>
        <p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
    </div>

</div><!-- /.container -->


<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://code.jquery.com/jquery-3.1.1.min.js" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>

<script>
    var currentPage = 1;
    var totalPerPage = 1;
    $(window).scroll(function() {
        if ($(document).height() <= ($(window).height() + $(window).scrollTop())) {
            next();
        }
    });
    function list() {
        $.getJSON("${baseUrl}api/vehicles/list?totalPerPage=" + totalPerPage + "&currentPage=" + currentPage, function( data ) {
            if (data.state != 'success') {
                alert('Error: ' + data.message);
                return;
            }

            $.each( data.vehicles, function( i, vehicle ) {
                var vehicle_title = vehicle.manufactureModel.manufacture.manufacture + ' ' + vehicle.manufactureModel.model.model;
                var card = $("<div></div>").attr("class", "card").attr("style", "width: 20rem;");
                var c_block = $("<div></div>").attr("class", "card-block");
                var c_group = $("<ul></ul>").attr("class", "list-group list-group-flush");
                var c_block_links = $("<div></div>").attr("class", "card-block");
                $("<img>").attr("class", "card-img-top").attr("src", vehicle.imageUpload.url).appendTo(card);
                $("<h4></h4>").attr("class", "card-title").text(vehicle_title).appendTo(c_block);
                $("<p></p>").attr("class", "card-text").text("A nice harley davidson vehicle").appendTo(c_block);
                $("<li><li>").attr("class", "list-group-item").text("Colour: " + vehicle.colour.colour ).appendTo(c_group);
                $("<li><li>").attr("class", "list-group-item").text("Manufacture Year:" + vehicle.manufactureYear ).appendTo(c_group);
                $("<li><li>").attr("class", "list-group-item").text("Engine Size:" + vehicle.engineSize).appendTo(c_group);
                $("<a></a>").attr("class", "card-link").text("View").appendTo(c_block_links);
                c_block.appendTo(card);
                c_group.appendTo(card);
                c_block_links.appendTo(card);

                $('#content').append(card);
            });
        });

    }

    function next() {
        currentPage++;
        list();
    }

    function previous() {
        if (currentPage <= 1)
            return;
        currentPage--;
        list();
    }

    list();

</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

上述情况正在发生,因为您没有关闭<li></li> 写在

$("<li><li>").attr("class", "list-group-item").text("Colour: " + vehicle.colour.colour ).appendTo(c_group);
$("<li><li>").attr("class", "list-group-item").text("Manufacture Year:" + vehicle.manufactureYear ).appendTo(c_group);
$("<li><li>").attr("class", "list-group-item").text("Engine Size:" + vehicle.engineSize).appendTo(c_group);

所以,它附加了两个

<li>