analysisDialog.vue
2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<template>
<el-dialog
:visible.sync="visible"
width="50%"
:before-close="handleClose"
:modal-append-to-body="false"
>
<div class="test-box" v-if="analysisUrl">
<iframe
:src="analysisUrl"
ref="iframe"
@load="onIFrameLoad()"
style="width: 100%; border: none; /* 启用点击穿透 */"
></iframe>
</div>
<span slot="title" class="title"> 题目解析 </span>
</el-dialog>
</template>
<script>
export default {
name: "analysisDialog",
props: {
visible: {
type: Boolean,
default() {
return false;
},
},
analysisUrl: {
// 科目
type: String,
default() {
return null;
},
},
},
data() {
return {};
},
watch: {
visible(val) {
if (val) {
console.log(val);
} else {
this.handleClose();
}
},
},
methods: {
/** 按钮 - 取消 */
handleClose() {
this.$emit("update:visible", false);
},
onIFrameLoad() {
const iframeRef = this.$refs.iframe; // 获取对应的 iframe
const doc = iframeRef.contentDocument || iframeRef.contentWindow.document;
const body = iframeRef.contentWindow.document.body;
body.style.overflowX = "hidden"; // 不允许出现横向滚动条
const height = body.scrollHeight; // 获取内容的高度
iframeRef.style.height = `${height + 20}px`; // 设置 iframe 的高度
// 获取第一个P标签
const firstP = doc.getElementsByTagName("p")[0];
// 或者修改第一个 < p > 标签的内容;
if (firstP) {
let a = this.processString(firstP.innerHTML);
firstP.innerHTML = a;
}
},
processString(input) {
// 正则表达式:匹配最多三位数字
const regex = /^(\d{1,3})/;
// 测试字符串是否以数字开头
const match = input.match(regex);
if (match) {
// 如果匹配,去掉开头的数字并返回剩余部分
return input.substring(match[0].length + 1).trim();
}
// 如果没有匹配,直接返回原字符串
return input;
},
},
};
</script>
<style lang="scss" scoped>
.title {
font-weight: 600;
}
.test-box {
width: 100%;
box-sizing: border-box;
padding: 20px;
}
</style>