效果图
实现代码
var showPdfView = document.getElementById('showPdfView')
showPdfView.onclick = function (e) {
var select_str = getSelectionField(e)
if (select_str === ''){
return
}
var textarea = document.getElementById('showPdfViewHtml')
if (textarea.value.indexOf(select_str) !== -1){
var parent_node = $(window.getSelection().getRangeAt(0).commonAncestorContainer.parentElement)
parent_node.addClass('select')
var node_arr = []
ergodic_ndoe(showPdfView)
function ergodic_ndoe(node) {
if(node.nodeType === 1){
if (node.children.length === 0){
if (node.innerHTML.indexOf(select_str) !== -1){
var index= -2;
var re = new RegExp(select_str, 'g')
var arr = node.innerHTML.match(re);
if (arr.length>1){
var position = 0
var anchorOffset = window.getSelection().anchorOffset
for (var i = 0;i<arr.length;i++){
index = node.innerHTML.indexOf(select_str, index) + select_str.length;
if (index === anchorOffset + select_str.length){
position = i
}
else if (index === anchorOffset){
position = i
}
}
node_arr.push({
el_select: node,
this_all_number: arr.length,
position: position
})
}
else {
node_arr.push({
el_select: node,
this_all_number: 1,
position: 0
})
}
}
}
else {
for (var i = 0;i<node.children.length;i++){
ergodic_ndoe(node.children[i])
}
}
}
}
var sign = 0
for (var j = 0;j< node_arr.length;j++){
if ($(node_arr[j].el_select).hasClass('select')){
sign += node_arr[j].position + 1
$(node_arr[j].el_select).removeClass('select')
break;
}
else {
sign += node_arr[j].this_all_number
}
}
var index2 = -2;
var re_r = new RegExp(select_str, 'g')
var arr_r = textarea.value.match(re_r);
if (arr_r && arr_r.length > 1){
for (var i = 0;i<arr_r.length;i++){
index2 = textarea.value.indexOf(select_str, index2) + select_str.length;
if ((i+1) === sign){
var line_break_str = textarea.value.substring(0,index2);
var line_break_end = textarea.value.substr(index2);
textarea.value = line_break_str
h1 = textarea.scrollHeight
textarea.value = line_break_str + line_break_end
textarea.scrollTop = h1 - 250
textarea.selectionStart = index2 - select_str.length;
textarea.selectionEnd = index2
break;
}
}
}
if (index2 === -2){
line_break_str = textarea.value.substring(0,textarea.value.indexOf(select_str));
line_break_end = textarea.value.substr(textarea.value.indexOf(select_str));
textarea.value = line_break_str
var h1 = textarea.scrollHeight
textarea.value = line_break_str + line_break_end
textarea.scrollTop = h1 - 250
textarea.selectionStart = textarea.value.indexOf(select_str);
textarea.selectionEnd = textarea.value.indexOf(select_str) + select_str.length
}
textarea.focus();
}
else {
alert('没有找到匹配项,请选中一个连续的字词')
}
}
function getSelectionField(e) {
var selection = '';
selection = getIeSelection(e);
if (selection == '') {
selection = getFireFoxSelection(e);
}
return selection.replace(/(^\s*)|(\s*$)/g, "");
}
function getIeSelection() {
if (window.getSelection) {
return window.getSelection().toString();
}
else if (document.getSelection) {
return document.getSelection();
} else if (document.selection) {
return document.selection.createRange().text;
}
}
function getFireFoxSelection(e) {
if (e.selectionStart != undefined && e.selectionEnd != undefined) {
var start = e.selectionStart;
var end = e.selectionEnd;
return e.value.substring(start, end);
} else {
return "";
}
}
model
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table class="tblborder" cellspacing="0" cellpadding="0" style="width: 100%;" border="0">
<tbody>
<tr>
<td>
<form name="form" id="myform" method="post" action="">
<input type="hidden" value="1" name="direction">
<input type="hidden" value="2" name="hear_foot">
<input type="hidden" value="0,0" name="pdf_header_or_footer">
<input type="hidden" value="486" name="tid">
<table cellspacing="0" cellpadding="0" width="100%" border="0">
<tbody>
<tr class="firstalt">
<td colspan="3">
</td><td colspan="5">
<div id="fixedShow">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr class="tbti">
<td width="100%" align="center" style="padding-left:10px;line-height: 40px;">
<button type="button" class="layui-btn layui-btn-sm" onclick="export_pdf()">導出PDF</button>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
<tr class=""><td colspan="6"> </td></tr>
<tr class="">
<td style="width:1%"> </td>
<td style="width:47%" align="left" valign="top">
<div id="showPdfView" class="left"><h2 style="text-align: center;"><span style="font-size: 8px;"><strong><span style="font-family: 黑体;"><span style="font-family: 黑体; font-size: 10px;"></span><htmlpageheader name="myHeader1"></htmlpageheader></span></strong></span></h2><h2 style="text-align: center;"><span style="