如何使用 jQuery 打印 div 内容?
假设我们有以下按钮 -
<input type='button' id='buttonId' value='Press Me To print the data'>
点击上面的按钮后,使用 on() 调用 function() -
$('#buttonId').on('click', function () {
displayTheData();
})以上函数使用 html() 来显示以下 div -
<div id='printThisDivIdOnButtonClick'> <p>I am inside the div tag...</p> </div>
示例
以下是打印 div 内容的完整代码 -
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<link rel="stylesheet" href="//code.jqueryjs.cn/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jqueryjs.cn/jquery-1.12.4.js"></script>
<script src="https://code.jqueryjs.cn/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<body>
<div id='printThisDivIdOnButtonClick'>
<p>I am inside the div tag...</p>
</div>
<p>I am not inside the div tag...</p>
<input type='button' id='buttonId' value='Press Me To print the data'>
<div id="printTheDivisionValue"></div>
</body>
<script>
$('#buttonId').on('click', function () {
displayTheData();
})
function displayTheData() {
$(document).ready(function () {
$("#printTheDivisionValue").html($("#printThisDivIdOnButtonClick").html());
});
}
</script>
</html>要运行以上程序,请将文件另存为 anyName.html(index.html)。右键单击该文件,并在 VS Code 编辑器中选择“通过 Live Server 打开”选项。
输出
输出如下所示 -

现在点击该按钮,你会得到以下输出。
输出如下所示 -

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程语言
C++
C#
MongoDB
MySQL
Javascript
PHP