如何在html javascript中使用此代码?克隆()

时间:2019-02-06 06:51:59

标签: javascript jquery html clone

我找到了此代码(示例https://jsfiddle.net/uxo27cq0/

function displaytickets(){
		var $panel = $('<div/>').addClass('col-xs-3 panel panel-default')
    $panel.append($('<div><h3 class="panel-title">Title</h3></div>').addClass('panel-heading'))
    $panel.append($('<div>Panel content</div>').addClass('panel-body'));
    $('body').append($panel);
    
  	// let's assume your ajax returns 4 tickets
    var data = {
    	'tickets' : [
      	{'description' : 'Description_1', 'status': 'Status_1', 'subject': 'Subject_1' },
      	{'description' : 'Description_2', 'status': 'Status_2', 'subject': 'Subject_2' },
      	{'description' : 'Description_3', 'status': 'Status_3', 'subject': 'Subject_3' },
      	{'description' : 'Description_4', 'status': 'Status_4', 'subject': 'Subject_4' }
      ]
    };
    
    for (i = 0; i < data.tickets.length; i++) {
    	var new_panel = $panel.clone(); // note the use of .clone()
      new_panel.find('.panel-title').text(data.tickets[i].subject);
      new_panel.find('.panel-body').text(data.tickets[i].description);
      $('body').append(new_panel);
		}
}

displaytickets();
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

我不知道如何通过以下方式使其在html中工作:

<html>
<style>
</style>
<body>

// div here

<script type="text/javascript">

// i put the code here

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

显然它不起作用,所以,如果有人可以告诉我应该怎么做,它将完全解决我的问题

谢谢!

4 个答案:

答案 0 :(得分:1)

<html>
<head>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head> 
<style>
</style>
<body>

// div here
</body>
<script type="text/javascript">

function displaytickets() {
    var $panel = $('<div/>').addClass('col-xs-3 panel panel-default')
                 $panel.append($('<div><h3 class="panel-title">Title</h3></div>').addClass('panel-heading'))
                 $panel.append($('<div>Panel content</div>').addClass('panel-body'));
$('body').append($panel);

  	// let's assume your ajax returns 4 tickets
     var data = {
	'tickets' : [
  	{'description' : 'Description_1', 'status': 'Status_1', 'subject': 'Subject_1' },
  	{'description' : 'Description_2', 'status': 'Status_2', 'subject': 'Subject_2' },
  	{'description' : 'Description_3', 'status': 'Status_3', 'subject': 'Subject_3' },
  	{'description' : 'Description_4', 'status': 'Status_4', 'subject': 'Subject_4' }
     ]
};

for (i = 0; i < data.tickets.length; i++) {
	var new_panel = $panel.clone(); // note the use of .clone()
  new_panel.find('.panel-title').text(data.tickets[i].subject);
  new_panel.find('.panel-body').text(data.tickets[i].description);
  $('body').append(new_panel);
	}
}

displaytickets();

</script>

</html>

答案 1 :(得分:0)

@Jess Yerena,这是您需要的-创建两个文件并运行index.html: index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <link
      href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"
      rel="stylesheet"
    />
  </head>
  <body>
    <div></div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="./main.js"></script>
  </body>
</html>

main.js

function displaytickets() {
  var $panel = $('<div/>').addClass('col-xs-3 panel panel-default');
  $panel.append(
    $('<div><h3 class="panel-title">Title</h3></div>').addClass('panel-heading')
  );
  $panel.append($('<div>Panel content</div>').addClass('panel-body'));
  $('body').append($panel);

  // let's assume your ajax returns 4 tickets
  var data = {
    tickets: [
      {
        description: 'Description_1',
        status: 'Status_1',
        subject: 'Subject_1'
      },
      {
        description: 'Description_2',
        status: 'Status_2',
        subject: 'Subject_2'
      },
      {
        description: 'Description_3',
        status: 'Status_3',
        subject: 'Subject_3'
      },
      { description: 'Description_4', status: 'Status_4', subject: 'Subject_4' }
    ]
  };

  for (i = 0; i < data.tickets.length; i++) {
    var new_panel = $panel.clone(); // note the use of .clone()
    new_panel.find('.panel-title').text(data.tickets[i].subject);
    new_panel.find('.panel-body').text(data.tickets[i].description);
    $('body').append(new_panel);
  }
}

displaytickets();

答案 2 :(得分:0)

尝试一下:在此处输入代码

$(document).ready(function(){
  displaytickets();
});
function displaytickets() {
  var $panel = $('<div/>').addClass('col-xs-3 panel panel-default')
  $panel.append($('<div><h3 class="panel-title">Title</h3></div>').addClass('panel-heading'))
  $panel.append($('<div>Panel content</div>').addClass('panel-body'));
  $('#div-clone-here').append($panel);

  // let's assume your ajax returns 4 tickets
  var data = {
    'tickets': [{
        'description': 'Description_1',
        'status': 'Status_1',
        'subject': 'Subject_1'
      },
      {
        'description': 'Description_2',
        'status': 'Status_2',
        'subject': 'Subject_2'
      },
      {
        'description': 'Description_3',
        'status': 'Status_3',
        'subject': 'Subject_3'
      },
      {
        'description': 'Description_4',
        'status': 'Status_4',
        'subject': 'Subject_4'
      }
    ]
  };

  for (i = 0; i < data.tickets.length; i++) {
    var new_panel = $panel.clone(); // note the use of .clone()
    new_panel.find('.panel-title').text(data.tickets[i].subject);
    new_panel.find('.panel-body').text(data.tickets[i].description);
    $('#div-clone-here').append(new_panel);
  }
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="div-clone-here">

</div>

答案 3 :(得分:0)

这对我有用

<html>
<head>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head> 
<style>
</style>
<body>

// div here
</body>
<script type="text/javascript">

function displaytickets() {
    var $panel = $('<div/>').addClass('col-xs-3 panel panel-default')
                 $panel.append($('<div><h3 class="panel-title">Title</h3></div>').addClass('panel-heading'))
                 $panel.append($('<div>Panel content</div>').addClass('panel-body'));
$('body').append($panel);

  	// let's assume your ajax returns 4 tickets
     var data = {
	'tickets' : [
  	{'description' : 'Description_1', 'status': 'Status_1', 'subject': 'Subject_1' },
  	{'description' : 'Description_2', 'status': 'Status_2', 'subject': 'Subject_2' },
  	{'description' : 'Description_3', 'status': 'Status_3', 'subject': 'Subject_3' },
  	{'description' : 'Description_4', 'status': 'Status_4', 'subject': 'Subject_4' }
     ]
};

for (i = 0; i < data.tickets.length; i++) {
	var new_panel = $panel.clone(); // note the use of .clone()
  new_panel.find('.panel-title').text(data.tickets[i].subject);
  new_panel.find('.panel-body').text(data.tickets[i].description);
  $('body').append(new_panel);
	}
}

displaytickets();

</script>

</html>

但是我想从代码中获取html元素,以便我可以自由编辑它们,然后使用id或其他方式调用它们。

<html>
<head>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head> 
<style>
 .panel {
   background-color: red;
   border-radius: none;
 }
 .heading {
   background-color: purple;
   width: 100%;

 }
 .panel-body {
   background-color: green;
 }
</style>
<body>

<div id="container" class="panel">
<div id="header" class="heading"><h3 class="panel-title">Title</h3></div>
<div id="content" class="panel-body">Panel content</div>

<script type="text/javascript">

function displaytickets() {
	var $panel = $('container')
	$panel.append($('header'));
	$panel.append($('content'));
	$('body').append(($panel));

	// let's assume your ajax returns 4 tickets
	var data = {
		'tickets': [
  	{'description' : 'Description_1', 'status': 'Status_1', 'subject': 'Subject_1' },
  	{'description' : 'Description_2', 'status': 'Status_2', 'subject': 'Subject_2' },
  	{'description' : 'Description_3', 'status': 'Status_3', 'subject': 'Subject_3' },
  	{'description' : 'Description_4', 'status': 'Status_4', 'subject': 'Subject_4' }
     ]
	};

	for (i = 0; i < data.tickets.length; i++) {
		var new_panel = $panel.clone(); // note the use of .clone()
		new_panel.find('.panel-title').text(data.tickets[i].subject);
		new_panel.find('.panel-body').text(data.tickets[i].description);
		$('body').append(new_panel);
	}
}

displaytickets();
</script>
</body>
</html>