vue中v-html 后端返回html + script js中click变乱不生效

打印 上一主题 下一主题

主题 554|帖子 554|积分 1662

结果图:

需求:点击加号执行后端返回的script中的代码

后端返回的html:

  1. <!DOCTYPE html>
  2. <html lang='zh'>
  3. <head>
  4. <title>xxx</title>
  5. <style>
  6.         body{font-size: 14px}
  7.         p{text-indent: 30px;}
  8.         textarea{width: 100%;}
  9.         input,textarea{box-sizing: border-box;}
  10.         label{cursor: pointer;}
  11.         th{border: none;}
  12.         td{border-style: solid; border-width: 1px;border-color:rgb(50%,50%,50%);}
  13.         table{border-collapse: collapse;width:26cm;}
  14.         td.noneBorder{border-style: none;}
  15.         .soloInput input{width: 100%;height: 100%;border-style: none none solid none;border-width: 1px}
  16.         .mixInput span{border-bottom: solid 1px;min-width: 100px;}
  17.         .addButton div{
  18.                 background-color: #4CAF50;
  19.                 border: none;
  20.                 color: white;
  21.                 padding: 5px 20px;
  22.                 text-align: center;
  23.                 display: inline-block;
  24.                 font-size: 16px;
  25.         }
  26. </style>
  27. </head>
  28. <body>
  29. <form name="form1">
  30. <table class='outTable'>
  31. <tr>
  32.         <th>&nbsp;</th>
  33.         <td align='center' colspan='3'>类型1</td>
  34.         <td colspan='3' class='soloInput'>
  35.                 <span th:if="${pdf}" th:text="${lsLx}"></span>
  36.                 <input th:if="${!pdf}" type='text' name='lsLx' th:value="${lsLx}">
  37.         </td>
  38.         <td align='center' colspan='3'>日期</td>
  39.         <td align='center' colspan='5' class='soloInput'>
  40.                 <span th:if='${pdf}' th:text="${nYr}"></span>
  41.                 <input th:if="${!pdf}" type='date' name='nYr' th:value="${nYr}">
  42.         </td>
  43. </tr>
  44. <tr>
  45.         <th>&nbsp;</th>
  46.         <td align='center' colspan='2' style='border-style:solid solid none solid;'>字段3</td>
  47.         <td align='center' style='border-style:none solid none solid;'>字段4</td>
  48.         <td align='center' rowspan='2' style='border-style:solid solid none solid;'>字段5</td>
  49.         <td align='center' rowspan='2' style='border-style:solid solid none solid;'>字段6</td>
  50.         <td align='center' rowspan='2' style='border-style:solid solid none solid;'>字段7</td>
  51.         <td align='center' colspan='8'>字段8</td>
  52. </tr>
  53. <tr>
  54.         <th>&nbsp;</th>
  55.         <td align='center' colspan='2' style='border-style:none solid none solid;'>编号</td>
  56.         <td align='center' style='border-style:none solid none solid;'>个数</td>
  57.         <td align='center' colspan='2'>1.0</td>
  58.         <td align='center' colspan='2'>2.0</td>
  59.         <td align='center'>3.0</td>
  60.         <td align='center'>4.0</td>
  61.         <td align='center'>5.0</td>
  62.         <td align='center'>6.0</td>
  63. </tr>
  64. <th:block th:each="_line_,_stat_ : ${check!=null?check:blankList5}">
  65. <tr>
  66.         <th>&nbsp;</th>
  67.         <td align='center' colspan='2' class='soloInput'>
  68.                 <span th:if="${pdf}" th:text="${_line_.item1}"></span>
  69.                 <input th:if="${!pdf}" type='text' name='check[].item1' th:value="${_line_.item1}">
  70.         </td>
  71.         <td align='center' class='soloInput'>
  72.                 <span th:if="${pdf}" th:text="${_line_.item2}"></span>
  73.                 <input th:if="${!pdf}" type='text' name='check[].item2' th:value="${_line_.item2}">
  74.         </td>
  75.         <td align='center' class='soloInput'>
  76.                 <span th:if="${pdf}" th:text="${_line_.item3}"></span>
  77.                 <input th:if="${!pdf}" type='text' name='check[].item3' th:value="${_line_.item3}">
  78.         </td>
  79.         <td align='center' class='soloInput'>
  80.                 <span th:if="${pdf}" th:text="${_line_.item4}"></span>
  81.                 <input th:if="${!pdf}" type='text' name='check[].item4' th:value="${_line_.item4}">
  82.         </td>
  83.         <td align='center' class='soloInput'>
  84.                 <span th:if="${pdf}" th:text="${_line_.item5}"></span>
  85.                 <input th:if="${!pdf}" type='text' name='check[].item5' th:value="${_line_.item5}">
  86.         </td>
  87.         <td colspan='2' class='soloInput'>
  88.                 <span th:if="${pdf}" th:text="${_line_.item6}"></span>
  89.                 <input th:if="${!pdf}" type='text' name='check[].item6' th:value="${_line_.item6}">
  90.         </td>
  91.         <td colspan='2' class='soloInput'>
  92.                 <span th:if="${pdf}" th:text="${_line_.item7}"></span>
  93.                 <input th:if="${!pdf}" type='text' name='check[].item7' th:value="${_line_.item7}">
  94.         </td>
  95.         <td class='soloInput' style='border-style:none solid solid solid;'>
  96.                 <span th:if="${pdf}" th:text="${_line_.item8}"></span>
  97.                 <input th:if="${!pdf}" type='text' name='check[].item8' th:value="${_line_.item8}">
  98.         </td>
  99.         <td class='soloInput' style='border-style:none solid solid solid;'>
  100.                 <span th:if="${pdf}" th:text="${_line_.item9}"></span>
  101.                 <input th:if="${!pdf}" type='text' name='check[].item9' th:value="${_line_.item9}">
  102.         </td>
  103.         <td class='soloInput' style='border-style:none solid solid solid;'>
  104.                 <span th:if="${pdf}" th:text="${_line_.item10}"></span>
  105.                 <input th:if="${!pdf}" type='text' name='check[].item10' th:value="${_line_.item10}">
  106.         </td>
  107.         <td class='soloInput' style='border-style:none solid solid solid;'>
  108.                 <span th:if="${pdf}" th:text="${_line_.item11}"></span>
  109.                 <input th:if="${!pdf}" type='text' name='check[].item11' th:value="${_line_.item11}">
  110.         </td>
  111. </tr>
  112. </th:block>
  113. <tr th:if="${!pdf}">
  114. <th>&nbsp;</th>
  115. <td colspan='14' class="addButton" style="text-align: center;"><div onclick="addcheck(this)">+</div>
  116. <script type="text/javascript">
  117. function addcheck(element){
  118.         var addTr=element.parentNode.parentNode;
  119.         var tableEle=addTr.parentNode;
  120.         var newElement=null;
  121.         newElement=document.createElement("tr");
  122.         newElement.innerHTML="        <th>&nbsp;</th>        <td align='center' colspan='2' class='soloInput'>                                <input type='text' name='check[].item1' value=''>        </td>        <td align='center' class='soloInput'>                                <input type='text' name='check[].item2' value=''>        </td>        <td align='center' class='soloInput'>                                <input type='text' name='check[].item3' value=''>        </td>        <td align='center' class='soloInput'>                                <input type='text' name='check[].item4' value=''>        </td>        <td align='center' class='soloInput'>                                <input type='text' name='check[].item5' value=''>        </td>        <td colspan='2' class='soloInput'>                                <input type='text' name='check[].item6' value=''>        </td>        <td colspan='2' class='soloInput'>                                <input type='text' name='check[].item7' value=''>        </td>        <td class='soloInput' style='border-style:none solid solid solid;'>                                <input type='text' name='check[].item8' value=''>        </td>        <td class='soloInput' style='border-style:none solid solid solid;'>                                <input type='text' name='check[].item9' value=''>        </td>        <td class='soloInput' style='border-style:none solid solid solid;'>                                <input type='text' name='check[].item10' value=''>        </td>        <td class='soloInput' style='border-style:none solid solid solid;'>                                <input type='text' name='check[].item11' value=''>        </td>";
  123.         tableEle.insertBefore(newElement,addTr);
  124. }
  125. </script>
  126. </td>
  127. </tr>
  128. </table>
  129. </form>
  130. </body>
  131. </html>
复制代码
现在如果直接用 v-html 放到vue 中 会报错;报这个方法没有;

办理思路:


1.获取到后端返回的html
2.通过正则表达式 之后 截取script 中的 js 逻辑
3.获取body标签 之后创建一个 script 标签
4.把我们截取的 script中的代码放到我们创建的 script标签中
5.把script标签 放到body中
实现:

html部分:

  1. <div v-html="htmlData"></div>
复制代码
vue js部分 

  1. export default {
  2. data() {
  3.     return {
  4.       htmlData: ""
  5.     }
  6.   },
  7.   mounted() {
  8.    // 我是取的本地存储的 你换成后端返回的就行
  9.     this.htmlData = localStorage.getItem('callbackHTML')
  10.    // 正则匹配
  11.     const regex = /<script type="text\/javascript">([\s\S]*?)<\/script>/g;
  12.     // let result = regex.exec(this.htmlData)  //获取匹配到的数据
  13.     // console.log(result);
  14.     // let result0 = regex.exec(this.htmlData)[0] // 0是带<script
  15. type="text\/javascript"> 这个的
  16.     // console.log(result);
  17.    let result1 = regex.exec(this.htmlData)[1]// 1是不带<script type="text\/javascript"> 这个的 我们用1
  18.     console.log(result1);
  19. //创建script标签
  20.     let script = document.createElement('script')
  21.     script.type = 'text/javascript'
  22.     script.text = result1
  23. //插入到body中
  24.     document.getElementsByTagName('body')[0].appendChild(script)
  25.   },
  26. }
复制代码
result1打印结果:


欣赏器展示script标签位置:


------------------------------------------------------end----------------------------------------------------------------------

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

锦通

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表