douzi7711 2018-05-14 21:37
浏览 43
已采纳

如何创建脚本以自动化HTML中的链接交换?

I'm performing maintenance on a system that has hundreds of links on one page as follows:

<li> Revista alvo <a href="http://exemplo.com"> http://exemplo.com </a> </li>

Note that there is text outside the a tag. What I want to do, put this text inside the a tag with your supposed href, the example above would look like this:

<li> <a href="http://exemplo.com"> Revista alvo </a> </li>

I'm doing this change in hand but there are hundreds and hundreds of links, which makes the task tiring. Does anyone have any idea how I can script to do this? It can be in any language, PHP,JS etc.

</div>
  • 写回答

1条回答 默认 最新

  • douziqian2871 2018-05-14 22:09
    关注

    As you regarded, the text is HTML format, so you can easily, edit the HTML file and use jquery to apply changes to the dom like the following:

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>JS Bin</title>
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    
    
    </head>
    <body>
    <ul id="linksList">
      <li> Revista alvo <a href="http://exemplo.com"> http://exemplo.com </a> </li>
      <li> Revista alvo <a href="http://exemplo.com"> http://exemplo.com </a> </li>
      <li> Revista alvo <a href="http://exemplo.com"> http://exemplo.com </a> </li>
      <li> Revista alvo <a href="http://exemplo.com"> http://exemplo.com </a> </li>
      <li> Revista alvo <a href="http://exemplo.com"> http://exemplo.com </a> </li>
      <li> Revista alvo <a href="http://exemplo.com"> http://exemplo.com </a> </li>
      <li> Revista alvo <a href="http://exemplo.com"> http://exemplo.com </a> </li>
      <li> Revista alvo <a href="http://exemplo.com"> http://exemplo.com </a> </li>
      <li> Revista alvo <a href="http://exemplo.com"> http://exemplo.com </a> </li>
      <li> Revista alvo <a href="http://exemplo.com"> http://exemplo.com </a> </li>
      <li> Revista alvo <a href="http://exemplo10.com"> http://exemplo.com </a> </li>
    
      </ul>
      <a href="#" id="changeIt">Change</a>
      <script>
        $(document).ready(function(){     
          $("#changeIt").click(function(){
            $("#linksList li").each(function(){
              txt = $(this).text().split(' http://')[0].trim();
              lnk = $(this).children('a').text(txt)
              $(this).html(lnk)
            })
          })
        })
      </script>
    </body>
    </html>
    

    The edit should include,

    1. include jquery
    2. give the ul or ol that contains the li an id
    3. use the code supplied in the latter script tag which consider the given id to the list is linksList
    4. HTML link with id changeIt to invoke the code.

    This is an Online demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法