用于将<li>行内的文本转换为搜索链接的脚本</li>

时间:2014-10-25 21:22:13

标签: javascript jquery html regex anchor

我里面有几个带有LI的DIV,但我想转换属于特定DIV id的LI里面的文本,例如:

<div id="filtro_mod">
  <ul>
    <li><strong>HP DesignJet 100 Printer series</strong>
      <ul>
        <li>HP DesignJet 100 Printer - C7796A</li>
        <li>HP DesignJet 100 Printer - C7796AR</li>
      </ul>
    </li>
    <li><strong>HP DesignJet 100 Plus Printer series</strong>
      <ul>
        <li>HP DesignJet 100 Plus Printer - C7796C</li>
        <li>HP DesignJet 100 Plus Printer - C7796CR</li>
      </ul>
    </li>
  </ul>
</div>

所以我假装转换它:

<li>HP DesignJet 100 Printer - C7796A</li>

进入:

<li><a href="http://mywebsite.com/pesquisa?controller=search&orderby=position&orderway=desc&search_query=HP%20DesignJet%20100%20Printer%20-%20C7796A+&submit_search=">HP DesignJet 100 Printer - C7796A</a></li>

然而在某些线路中,LI内部存在STRONG,其他LIs。例如:

<ul>
  <li><strong>HP DesignJet 100 Printer series</strong>
    <ul>
      <li>HP DesignJet 100 Printer - C7796A</li>
      <li>HP DesignJet 100 Printer - C7796AR</li>
    </ul>
  </li>
</ul>

我认为这可以像脚本一样将普通网址文本转换为这样的链接:

How to replace plain URLs with links?

1 个答案:

答案 0 :(得分:3)

$('#filtro_mod li li').each(function(){
    var html = $(this).html();
    $(this).html('<a href="theurl.com?desc=' + html + '">' + html + '</a>');
});