简体   繁体   English

在ul中访问特定的li

[英]Accessing a specific li within a ul

After looking at past examples and source code I have made, I can't seem to work out accessing a specific <li><a>the value in this</a></li> based on a parameter sent in. 看完过去的示例和我编写的源代码后,我似乎无法根据发送的参数来计算访问特定<li><a>the value in this</a></li>中的值。

Mockup: 小样:

  <ul class="selectBox-dropdown-menu selectBox-options size-dropdown mediumSelect footwear" style="top: 309px; left: 34px; display: none;">
    <li class="" style="display: none;"><a rel=""></a></li>
    <li class="size-not-in-stock"><a rel="3096786:6"> 6</a></li>
    <li class="size-not-in-stock"><a rel="3096787:6.5"> 6.5</a></li>
    <li class=""><a rel="3096788:7"> 7</a></li>
    <li class="selectBox-selected"><a rel="3096789:7.5"> 7.5</a></li>
    <li class=""><a rel="3096790:8"> 8</a></li>
    <li class=""><a rel="3096791:8.5"> 8.5</a></li><li><a rel="3096792:9"> 9</a></li>
    <li><a rel="3096793:9.5"> 9.5</a></li><li><a rel="3096794:10"> 10</a></li>
    <li><a rel="3096795:10.5"> 10.5</a></li><li><a rel="3096796:11"> 11</a></li>
    <li><a rel="3096797:11.5"> 11.5</a></li><li><a rel="3096798:12"> 12</a></li>
    <li><a rel="3096799:12.5"> 12.5</a></li><li><a rel="3096800:13"> 13</a></li>
    <li><a rel="3096801:14"> 14</a></li><li><a rel="3096802:15"> 15</a></li></ul>
  </ul>

Here is what I am trying to get. 这就是我想要得到的。 Let us say that a user puts in a value of 7, well than it should find the corresponding <li><a></a></li> that contains the number 7 and click it. 假设用户输入的值为7,好于它应该找到包含数字7的相应<li><a></a></li>并单击它。

My troubles are with finding that value inside this, I know I need to use find within the <ul> but what stumps me is based on a value. 我的麻烦是要在其中找到该值,我知道我需要在<ul>使用find,但是让我感到沮丧的是基于一个值。

UPDATE: 更新:

I just want to make clear that, this is something that is going to be an auto process so I am trying to make it so I don't have to do anything except load the page. 我只想澄清一下,这将是一个自动过程,因此我正在尝试做到这一点,因此除了加载页面外,我无需执行任何操作。

You need something like 你需要类似的东西

var test = "the content to seek";
$('ul.footwear').find('a').filter(function(idx, el){
    return $.trim($(this).text()) == $.trim(test);
}).closest('li').trigger('click')

Demo: Fiddle 演示: 小提琴

There is no need to loop through and read the innerHTML of every element like all of the other solutions appear to be doing. 像其他所有解决方案一样,无需遍历并读取每个元素的innerHTML。 You can just do it with a selector. 您只需使用选择器即可。

Since the rel attribute seems to have the data you are after at the end :size , you can use use :has() and ends with $= selectors to get the lis you are after. 由于rel属性似乎在:size结尾处具有您要获取的数据,因此可以使用use :has()$=选择器结尾来获取您要获取的lis。

var num = 7;
var elems = $(".footwear li:has(a[rel$=':" + num + "'])");
console.log(elems.length);

And if you want to click it, than you call .click() or .trigger("click") 如果要单击它,则可以调用.trigger("click") .click().trigger("click")

function findAndClick (size) {
    var elem = $(".footwear li:has(a[rel$=':" + size + "'])");
    elem.trigger("click");
} 

And to trigger it on the page load it would be something like 并在页面加载时触发它就像

$(window).on("load", function() { findAndClick(7); } );

or document ready 或准备好文件

$( function() { findAndClick(7); } );

Sad thing is, this solution appears to be great with a simple selector, but the performance can be subpar. 可悲的是,使用简单的选择器,该解决方案似乎很棒,但性能却不如预期。 If there is only going to be one element with the size, the best performance would be an each() loop and breaking out of it when you find the one element. 如果只有一个元素具有该大小,则最佳性能将是一个each()循环,并在找到一个元素时中断该循环。 No need to look at the other elements. 无需查看其他元素。


The best performing would be an each() 表现最好的是each()

function findAndClick (size) {
    var elem;
    size = size.toString();
    $('.footwear').find('a').each(function () {
        if ($.trim($(this).text()) == size) {   //compare the text
            elem = $(this).parent();  //set the element that contains the link 
            return false;  //exit each loop
        }
    });

    if (elem) {
        elem.trigger("click");  //fire click
    }
}

For even better performance, eliminate the need to read the text of the element or use a ends with selector. 为了获得更好的性能,无需阅读元素文本或使用带选择符的结尾。 Add a data attribute to the element. 将数据属性添加到元素。

<li data-size="7">

and than you would just use a selector 比起您只需要使用一个选择器

function findAndClick (size) {
    $('.footwear li[data-size="' + size + '"]').trigger("click");
}

I would optimize this by making your structure better suited for these types of queries 我将通过使您的结构更适合这些类型的查询来优化此设置

<li id="10"><a rel="3096793:9.5"> 9.5</a></li><li><a rel="3096794:10"> 10</a></li>

Put an id on the li that corresponds to the value of the a tags. 在与a标记值相对应的li上放置一个id。

Then you can do a simple $("#10") selector 然后,您可以做一个简单的$("#10")选择器

While it's possible to make complex selectors based on filters etc, keep in mind that performance will not be great in general for non browser backed selectors (pseudo selectors) 虽然可以根据过滤器等制作复杂的选择器,但请记住,对于非浏览器支持的选择器(伪选择器),性能通常不会很好

由于您拥有一个以内容rel属性,因此可以使用此属性:

$('a[rel$="\:'+valueToMatch+'"]').parent()

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

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