简体   繁体   English

jQuery / HTML Operated在找到的元素上

[英]jQuery/HTML Operate on the found element

This is my first question here. 这是我的第一个问题。 First of all, please see this bit of code. 首先,请参阅以下代码。

 $('#btn').click(function () {
 if ($(this).parent().find('a').attr('href') == $("#search_list").find('a').attr('href')) {
  $(secondelement).css(.......) ///any operation.
 }
 });

My question is how to perform an operation on the found/matched/second element? 我的问题是如何对found / matched / second元素执行操作? In this case, the element 'a' in #search_list having href equal to the href of 'a' in #btn's parent. 在这种情况下,#search_list中的元素'a'的href等于#btn父级中的'a'的href。

What you have is 你拥有的是

$(this).parent().find('a').attr('href') == $("#search_list").find('a').attr('href')

it gets the href attribute of the first found anchor in the parent and compares against the first found anchor inside #search_list ( attr will return the attribute for the first element in the collection). 它获取父级中第一个找到的锚的href属性,并与#search_list中的第一个锚进行比较( attr将返回集合中第一个元素的属性)。

To get the same element, you can use the attributes selector instead, and select the first anchor in #search_list if it has the same href as the first anchor in the parent. 要获得相同的元素,可以改为使用属性选择器,如果#search_list中的第一个锚与父级中的第一个锚具有相同的href,则选择它。

EDIT: to get any anchor with the same href as the first anchor in the parent element, you'd do : 编辑:要获取具有与父元素中的第一个锚相同的href的任何锚,您可以这样做:

$('#btn').on('click', function () {
    $('#search_list a[href="' + $(this).parent().find('a').attr('href') + '"]').css('color', 'red');
});

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

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