如何使用HTML的JavaScript代码?克隆()

问题描述 投票:0回答:4

我发现这个代码(实施例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>

显然它没有工作,所以,如果有人能告诉我应该如何,这将彻底解决我的问题

谢谢!!!

javascript jquery html clone
4个回答
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>

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();

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>

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>
© www.soinside.com 2019 - 2024. All rights reserved.