简体   繁体   English

jQuery-根据<A>标签</a>内的文本执行匹配

[英]JQuery - Perform matching based on text inside <A> Tags

I have had a look at; 我看了看; Jquery Extract URL from Text and jquery match() variable interpolation - complex regexes but I'm still a bit lost. jQuery从Textjquery match()变量插值中 提取URL- 复杂的正则表达式,但我仍然有些失落。

The problem; 问题; I have a page that contains a drop down menu along the top, inside the drop down is the name of an item which I can click on to view that item. 我有一个页面,该页面的顶部包含一个下拉菜单,在下拉菜单中是项目的名称,我可以单击该项目以查看该项目。 The URL of the item is unrelated to the name, it is a unique ID. 项目的URL与名称无关,它是唯一的ID。

Example code of dropdown; 下拉代码示例;

    <div class="innerMenuWrapper">
    <li class="hasSubMenu sfhover">
    <a href="javascript:void(0);" tabindex="-1" class="menuItemLink">Items</a>
    <span class="splIcon splIcon-triangle-4-e dropDown">Submenu</span>
    <div class="outerMenuWrapper splShadow" style="left: 160px; display: none; ">
    <ul>
    <div class="innerMenuWrapper">
    <li class="">
    <a href="/en-US/app/Items/itemahdwhidwbow" tabindex="-1"
       class="menuItemLink">item.one</a>
    </li>
    <li class="">
    <a href="/en-US/app/Items/itemfhfaogsgs" tabindex="-1"
       class="menuItemLink">item.two</a>
    </li></div></ul></div></li></div>

This is all generated externally and managed by the actual web service itself. 所有这些都是从外部生成的,并由实际的Web服务本身进行管理。 On the page itself is a table generated from a dataset. 在页面本身上是从数据集生成的表。 I have generated the data with no problem and presentation is fine as-well. 我生成的数据没有问题,演示也很好。 What I want to do is to pull the URL from the HREF above and to wrap the same item in the table on the main page. 我要做的是从上面的HREF中提取URL,并将同一项目包装在主页上的表中。

Eg I have a table of 3 columns, Items, Item contents, Item price etc. I want to wrap each Item with its associated URL from the above. 例如,我有一个由3列组成的表格,分别是商品,商品内容,商品价格等。我想将每个商品及其相关的网址包装在上面。

So to take item.one as an example, that is the name it has in the drop down and has the URL, /en-US/app/Items/itemahdwhidwbow . 以item.one为例,它是下拉列表中的名称,并具有URL /en-US/app/Items/itemahdwhidwbow Inside the data itself I have this code to select each first cell of the table and wrap the contents with a link (part of a larger code-set); 在数据本身内部,我有这段代码可以选择表的每个第一个单元格,并用链接(大代码集的一部分)包装内容。

if (tr.find("td:nth-child(1)")) {
  tr.find("td:nth-child(1)").wrapInner(function() {
    var link = $('<a/>');
    link.attr('href', 'US/app/Items/itemahdwhidwbow');
    link.text($(this));
    return link;
  });
}

Obviously at the moment the URL is static. 显然,此刻URL是静态的。 What I really need is a way to pull the URL from the associated link in the drop down on the same page. 我真正需要的是一种从同一页面上下拉菜单中的关联链接中提取URL的方法。 The name in the first table cell will always match the name in the drop down list, is there a simple way for me to do this? 第一个表格单元格中的名称将始终与下拉列表中的名称匹配,请问有一种简单的方法吗? What I have read so far points towards regex use but in this case I am looking for the contents of one part of the code to match something further down the page. 到目前为止,我所读的内容都指向正则表达式,但在这种情况下,我正在寻找代码一部分的内容,以使其与页面下端的内容匹配。

EDIT: 编辑:

So for example, item.one has the text "item.one". 例如,item.one的文本为“ item.one”。 "item.one" is also in the table, I want the "item.one" in the table to have the same link as in the drop down. 该表中也包含“ item.one”,我希望该表中的“ item.one”具有与下拉列表相同的链接。 If I could get to the text inside the anchor then I could just do a match against the text in the table 如果我可以找到锚点内的文字,则可以与表格中的文字进行匹配

I'm not clearly understand your table layout structure but i can give you a general idea: 我不清楚您的表格布局结构,但可以给您一个大致的概念:

$('a.menuItemLink').each(function(index, linkEl){ //Iterate over dropdown links      
     var $link=$(linkEl);
     tr.find("td:nth-child("+ index +")") //Lets say here we get required cell by index
         .wrapInner( 
              $('<a/>').attr('href', $link.attr('href'))
         );
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM